【问题标题】:Changing the visibility of a textview in a listview更改列表视图中文本视图的可见性
【发布时间】:2012-11-26 00:47:27
【问题描述】:

我有一个列表视图,它由来自单独布局文件的两个文本视图组成。我使用 BaseAdapter 从 JSON 文件构建列表。

我希望第一个文本视图(标题)是可点击的,如果单击它会显示第二个文本视图(文本),如果再次单击它会隐藏它。

当我使用onClick (android:onClick="ClText") 时出现错误。我想我应该使用onClickListener,但由于我是 Android 新手,我不太确定如何使用它。

有人可以帮我写代码吗?

【问题讨论】:

  • 只在代码中使用onClickListener,因为onclick属性会导致很多问题
  • 好的,但是我如何识别正确的视图呢?由于 ListView 中的所有视图都具有相同的 ID(mTxtvCaption 和 mTxtvText),我需要它来识别已单击的正确 mTxtvCaption 并使正确的 mTxtvText 可见
  • @Tino,您需要确保为您的文本视图提供唯一 ID。
  • 我知道,但由于它是一个列表视图,因此文本视图会重复多次...
  • @Tino,当在适配器的 getView 方法中创建列表中的每个父视图时,TextViews 之间的这种区别是针对列表中的每个父视图进行的。您必须在那里处理标题 TextView 的 onClick。请参阅我的答案以获取代码示例。

标签: android listview onclick visibility


【解决方案1】:

您只需为扩展 BaseAdapter 的适配器类的 getView 方法中的第一项设置 onClickListener。这里有一个例子来说明你想要做什么。

public class CustomAdapter extends BaseAdapter{
    private ArrayList<Thing> mThingArray;

    public CustomAdapter(ArrayList<Thing> thingArray) {
        mThingArray = thingArray;
    }

    // Get the data item associated with the specified position in the data set.
    @Override
    public Object getItem(int position) {
        return thingArray.get(position);
    }

    // Get a View that displays the data at the specified position in the data set.
    // You can either create a View manually or inflate it from an XML layout file.
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            // LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.one_of_list, null);
        }

        TextView captionTextView = (TextView) convertView.findViewById(R.id.caption);
        TextView txt2 = (TextView)findViewById(R.id.text);

        captionTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           if(txt2.getVisibility() == View.INVISIBLE){
           txt2.setVisibility(View.VISIBLE);
        } else {
           txt2.setVisibility(View.INVISIBLE);
        }
       }
    });

        return convertView;
    }
}

【讨论】:

    【解决方案2】:

    如果你只是想在两个 textview 之间切换,你可以简单地使用 ViewSwitcher。它允许您在其中的视图之间切换。你只需要调用nextView()方法,这个方法是循环的,所以你可以无休止地调用nextView()

    您将能够更改显示的视图。

    然后您可以将相同的 onClickListener 添加到这些文本视图中,编写如下内容:

    TextView txt = (TextView )findViewById(R.id.TextView1);
    
    txt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
          viewSwitcher.nextView();
    }
    });
    

    【讨论】:

      【解决方案3】:

      这是一个关于如何在 java 中使用点击监听器的示例:

      TextView txt = (TextView )findViewById(R.id.TextView1);
      TextView txt2 = (TextView )findViewById(R.id.TextView2);
      txt.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              txt.setVisibility(View.GONE);
              txt2.setVisibility(View.VISIBLE);
          }
      });
      

      老实说,与其改变不同 textView 的可见性,不如直接改变 TextView 文本?它会简单得多,并且不需要多个 TextView。

      【讨论】:

      • 谢谢!我到了:) 我必须使用多个文本视图,因为 JSON 包含许多与文本匹配的标题。在启动时,应用程序只需要显示标题,并且必须显示匹配的文本。
      猜你喜欢
      • 2019-09-02
      • 2014-07-04
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 2014-08-04
      • 1970-01-01
      相关资源
      最近更新 更多