实现的方法是将ListView 的Item设置为显示和隐藏两个部分,点击显示界面后,才会显示隐藏的详情页信息,具体流程和代码如下:
1.activity_main.xml的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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" tools:context="com.example.mylistview.MainActivity">
<ListView
android:id="@+id/id_listview"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"></ListView>
</android.support.constraint.ConstraintLayout>
2.listview_item.xml的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:id="@+id/id_text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="12321321显示部分"/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="150dp">
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="@color/colorGrey"/>
<TextView
android:id="@+id/id_text2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="12321321隐藏部分"/>
</LinearLayout>
</LinearLayout>
3.ListView的数据列
public class ListData {
private String text1;
private String text2;
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
public String getText1() {
return text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
}
4.ListViewAdapter(关键解决位置):
public class ListViewAdapter extends BaseAdapter {
private List<ListData> mData;
private Context context;
private int currentItem = -1;//listview中显示位置,取默认值为-1。
@Override
public int getCount() {
return mData.size();
}
public ListViewAdapter(Context context,List<ListData> mData){
this.context = context;
this.mData = mData;
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.listview_item,null);
holder = new ViewHolder();
holder.textView1 = (TextView)convertView.findViewById(R.id.id_text1);
holder.textView2 = (TextView)convertView.findViewById(R.id.id_text2);
holder.linear1 = (LinearLayout)convertView.findViewById(R.id.linear1);
holder.linear2 = (LinearLayout)convertView.findViewById(R.id.linear2);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.textView1.setText(mData.get(position).getText1());
holder.textView2.setText(mData.get(position).getText2());
/**
* 判断currentItem与position是否相等,首次加载进来的时候,因为currentItem为-1,则不会加载。
* 点击后刷新listview再次比对,相等后加载隐藏信息。
*/
if(currentItem == position){
holder.linear2.setVisibility(View.VISIBLE);
}else {
holder.linear2.setVisibility(View.GONE);
}
holder.linear1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//点击linear后将点击的position位置信息赋值给currentItem,刷新listview。
int tag = position;
if(tag == currentItem){
currentItem = -1;
}else{
currentItem = tag;
}
notifyDataSetChanged();
}
});
return convertView;
}
public class ViewHolder{
TextView textView1;
LinearLayout linear1;
LinearLayout linear2;
TextView textView2;
}
}
5.在Activity中声明:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<ListData> mData;
private ListViewAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getData();
initView();
}
private void getData() {
mData = new ArrayList<>();
for(int i = 0;i < 10;i++) {
ListData data = new ListData();
data.setText1("213213");
data.setText2("隐藏");
mData.add(data);
}
}
private void initView() {
listView = (ListView)findViewById(R.id.id_listview);
mAdapter = new ListViewAdapter(this,mData);
listView.setAdapter(mAdapter);
}
}
注意:关键性代码在ListViewAdapter中,其他位置就是简单的listview的显示。