从某个元素访问ListView的其他元素的视图

时间:2013-11-18 09:23:27

标签: android android-listview

我有一个ListView,每个元素都是自定义布局。布局由两个框架组成:第一个是TextView,第二个是LinearLayout,由两个图像组成。我想要做的是,如果我点击ListView的元素,如果LinearLayout“消失”,它将“可见”并且TextView被禁用。如果LinearLayout是“可见的”,则会“消失”,TextView会被禁用。这是我的以下代码:

代码:

  ListView lvw = (ListView) layout.findViewById(R.id.formats);
            formatAdapter adapter = new formatAdapter(act, arr);    //act is context and arr is an array of String
            lvw.setAdapter(adapter); 
            lvw.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    TextView tev = (TextView) arg1
                            .findViewById(R.id.formatN);
                    LinearLayout extraB = (LinearLayout) arg1
                            .findViewById(R.id.extraButtons);
                    if (extraB.getVisibility() == View.GONE) {
                        extraB.setVisibility(View.VISIBLE);
                        tev.setEnabled(false);
                    } else {
                        extraB.setVisibility(View.GONE);
                        tev.setEnabled(true);
                    }

                }
            });

formatAdapter.java

public class formatAdapter extends BaseAdapter {
LayoutInflater Inflater;
Context con;
String[] names;

public formatAdapter(Context c, String[] s) {
    con = c;
    names = s;
    Inflater = (LayoutInflater) con
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    return names.length;
}

@Override
public Object getItem(int arg0) {
    return null;
}

@Override
public long getItemId(int arg0) {
    return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    View v1 = arg1;
    v1 = Inflater.inflate(R.layout.adapter_formats, null);
    TextView tv = (TextView) v1.findViewById(R.id.formatN);
    tv.setText(names[arg0]);
    return v1;
}

}

adapter_formats.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/formatN"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp" />

<LinearLayout
    android:id="@+id/extraButtons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal" 
    android:visibility="gone">

    <ImageView
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/cD"
        android:src="@drawable/img_29" />

    <View
        android:layout_width="50dp"
        android:layout_height="0dp"
        android:contentDescription="@string/cD" />

    <ImageView
        android:id="@+id/download"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_31" />
</LinearLayout>

</FrameLayout>

现在,我希望如果对于任何元素,LinearLayout是可见的,如果我点击其他元素,那么元素LinearLayout就会消失,TextView会被启用(默认布局)。让我用例子解释一下:如果我克隆了第一个元素,它的LinearLayout就可见了。我希望如果我点击第二个元素,第一个元素的线性布局会自动消失。我试着解释得很好,但对不起,如果我无法解释好。任何人都可以帮我这个吗?

3 个答案:

答案 0 :(得分:1)

刚刚找到一种方法来做到这一点,我觉得很棒:)尽管这种方式不是真的:P 为了隐藏其他列表的视图,我在我的类中全局访问了一个视图并将其初始化为null(视图是我要隐藏的同一个类,例如,这里我想使前一个列表元素的线性布局消失,所以我采取了全局线性布局)。然后在每个onItemClickListener中,我将以下代码放在开头:

if(myGlobalView!=null)
    {
    myGlobalView.hide();
    }

此外,当我使其他列表元素的视图可见时,我通过以下代码将该视图分配给我的全局视图:

myGlobalView = myCurrentView;

希望它可以帮助其他人:)

答案 1 :(得分:1)

在适配器中,按住要显示LinearLayout的position

public class formatAdapter extends BaseAdapter {
    LayoutInflater Inflater;
    Context con;
    String[] names;

    // Position to show
    private int positionToShow;

    // Constants

    // Default - All LinearLayouts are hidden
    final static int NO_CURRENT_SELECTION = -1

    public formatAdapter(Context c, String[] s) {
        con = c;
        names = s;
        Inflater = (LayoutInflater) con
                              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // Initial setting
        positionToShow = NO_CURRENT_SELECTION;
    }

    // Updates 'positionToShow' - used in ListView's OnItemClickListener
    public void setPositionToShow(int newPosition) {
        positionToShow = newPosition;
    }

    // Current position for which the LinearLayout should be visible
    public int getPositionToShow() {
        return positionToShow;
    }
}

在ListView的OnItemClickListener中,使用我们在上面定义的positionToShow方法更新setPositionToShow()

lvw.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        // User has clicked the same position again - toggle visibility
        if (yourAdapter.getPositionToShow() == arg2) {

            // No LinearLayouts should be visible    
            yourAdapter.setPositionToShow(
                                       FormatAdapter.NO_CURRENT_SELECTION);            
        } else { // User has clicked a new position

            // Show LinearLayout for this position
            yourAdapter.setPositionToShow(arg2);             
        }

        // Refresh the ListView    
        yourAdapter.notifyDataSetChanged();           
    }
});

最后,在适配器的getView(...)方法中,使用positionToShow来管理观看次数的可见性:

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    View v1 = arg1;
    v1 = Inflater.inflate(R.layout.adapter_formats, null);
    TextView tv = (TextView) v1.findViewById(R.id.formatN);

    // Initialize the LinearLayout
    LinearLayout ll = (LinearLayout) v1.findViewById(R.id.extraButtons);

    tv.setText(names[arg0]);

    // This position's LinearLayout should be visible
    if (positionToShow == arg0) {

        ll.setVisibility(View.VISIBLE);
        tv.setEnabled(false);

    } else { // Either the position is not in view, 
             // or 'positionToShow' is set to NO_CURRENT_SELECTION

        ll.setVisibility(View.GONE);
        tv.setEnabled(true);
    } 

    return v1;
}

答案 2 :(得分:1)

这里的技巧是保持所选位置,以便在单击下一个列表项时知道需要修改哪个视图。

您可以使用将保持当前所选位置的类级变量

private int mCurrentPosition = -1;

您可以使用以下代码替换ListView onItemClick()方法。

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    TextView tev = (TextView) arg1.findViewById(R.id.formatN);
    LinearLayout extraB = (LinearLayout) arg1
            .findViewById(R.id.extraButtons);
    if (mCurrentPosition > -1) {
        View v = arg0.getChildAt(mCurrentPosition);
        v.findViewById(R.id.extraButtons).setVisibility(View.GONE);
    }
    if (extraB.getVisibility() == View.GONE) {
        extraB.setVisibility(View.VISIBLE);
        tev.setEnabled(false);
    } else {
        extraB.setVisibility(View.GONE);
        tev.setEnabled(true);
    }
    mCurrentPosition = arg2;
}

这应该可以解决您的问题。快乐编码:)