android - 从某个元素访问 ListView 的其他元素的 View

标签 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);
                    }

                }
            });

格式适配器.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;
}

}

适配器格式.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 就会变得可见。我希望如果我点击第二个元素,第一个元素的线性布局会自动消失。我试图解释得很好,但如果我无法解释得很好,我很抱歉。谁能帮我解决这个问题?

最佳答案

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

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

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

myGlobalView = myCurrentView;

希望对其他人有帮助:)

关于android - 从某个元素访问 ListView 的其他元素的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20044145/

相关文章:

android - 错误 "E/MultiWindowProxy: getServiceInstance failed!"是什么意思?

Android - Listview with viewholder 在按下时更改所选行的背景

java - 处理多个 Intent 并同步数据

android - 未授予 READ_PRIVILEGED_PHONE_STATE 权限

安卓 ListView : how to notify datasource when delete one row?

android - Unity Ads 无法显示广告 : webapp not initialized

android - 我怎样才能插入到 Stringp[

android - 如何处理长按 ListView 项目?

android - 错误 : Attempted to access a cursor after it has been closed?

Android:扫描 Wifi 网络 + 可选列表