listView包含组的项目

时间:2018-01-23 17:52:01

标签: android listview

我有ListView BaseAdapter现在,当我点击项目时,我只是对项目进行了一些监听。我在项目中显示了一些隐藏的视图。

每次点击某个项目时,如果视图可见,我会显示隐藏的视图或隐藏它们。但我面临的问题是,当我展示新项目时,我想要隐藏其他可见项目。

在这里,我点击了一些项目,它显示了消息的日期和详细信息:

screenshot 1

现在当我点击其他项目时,我想隐藏第一个项目并显示第二项,如下所示:

screenshot 2

但是在我的代码中,当我点击其他项目时,它当然不会隐藏上一个视图。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

只需保留对点击的最后一个视图的引用,我们称之为mLastViewShown;

new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mLastViewShown != null)
            hideDetails();
        mLastViewShown = view;

        showDetails();
    }
};

现在您有一个变量来隐藏上次用户点击的详细信息。

答案 1 :(得分:0)

因此,当您点击其他项目时,您希望隐藏所有项目的附加字段。除此之外,您还希望在点击相同项目时切换项目的附加视图的可见性。 这是您的主要活动:

 public class MainActivity extends AppCompatActivity {

private List<ListData> list=new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //get the toolbar instance

    handleListClick();
    list.add(new ListData("George","Surgeon"));
    list.add(new ListData("Nancy","Dentist"));
    list.add(new ListData("Henry","Nurse"));

}

private void handleListClick() {
    ListView listView=findViewById(R.id.listView);
    final CustomBaseAdapter customBaseAdapter=new CustomBaseAdapter(this,list);
    listView.setAdapter(customBaseAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            customBaseAdapter.setCurrentSelected(position);
            customBaseAdapter.notifyDataSetChanged();
        }
    });
   }
 }

这是您的自定义基本适配器:

    public class CustomBaseAdapter extends BaseAdapter {
    private  List<ListData> myList = new ArrayList<ListData>();
    private  LayoutInflater inflater;
    private  Context context;
    private int previousSelected=-1;

    public CustomBaseAdapter(Context context, List<ListData> myList) {
    this.myList = myList;
    this.context = context;
    inflater = LayoutInflater.from(this.context);
   }

    public void setCurrentSelected(int currentSelected){
    if(previousSelected==currentSelected)
        previousSelected=-1;
    else
        previousSelected=currentSelected;
    }

    @Override
    public int getCount() {
    return myList.size();
    }

    @Override
    public ListData getItem(int position) {
    return myList.get(position);
    }

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

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
     MyViewHolder mViewHolder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.row_item, parent, false);
        mViewHolder = new MyViewHolder(convertView);
        convertView.setTag(mViewHolder);
    } else {
        mViewHolder = (MyViewHolder) convertView.getTag();
    }

    ListData currentListData = getItem(position);

     mViewHolder.tvMain.setText(currentListData.getMainText());
     if(previousSelected==position){
        mViewHolder.tvAdditional.setVisibility(TextView.VISIBLE);

   mViewHolder.tvAdditional.setText(currentListData.getAdditionalText());
    }else{
        mViewHolder.tvAdditional.setVisibility(TextView.GONE);
    }

    return convertView;
    }

     private class MyViewHolder {
    TextView tvMain, tvAdditional;


    public MyViewHolder(View item) {
        tvMain = (TextView) item.findViewById(R.id.main_text);
        tvAdditional = (TextView) item.findViewById(R.id.additional_text);

      }
    }
 }

这是你的ListData:

  public class ListData {
   private String mainText;
   private String additionalText;

    public ListData(String mainText, String additionalText) {
    this.mainText = mainText;
    this.additionalText = additionalText;
    }

    public String getMainText() {
    return mainText;
    }

   public void setMainText(String mainText) {
    this.mainText = mainText;
   }

   public String getAdditionalText() {
    return additionalText;
   }

  public void setAdditionalText(String additionalText) {
    this.additionalText = additionalText;
  }
 }

这是你的row_item.xml布局文件:

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

    <TextView
    android:id="@+id/main_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    <TextView
    android:id="@+id/additional_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

</LinearLayout>

这是activity_main.xml布局文件:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.dexter.stackoverflow.MainActivity">


    <ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
   </LinearLayout>