将其更改为不同的布局后如何单击listview项?

时间:2016-03-15 06:46:13

标签: android listview layout-inflater

当我点击listview时,它会将该项目更改为不同的布局。但在那之后,我再也无法点击该项目了。我应该做些什么?请帮我。谢谢。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null || convertView.getTag() == null) {
        convertView = inflater.inflate(R.layout.activity_mainlist_item, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);

    }else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.rl_item.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectedItem(position);
            notifyDataSetChanged();
            Intent intent = new Intent(mContext.getApplicationContext(), DetailActivity.class);
            mContext.startActivity(intent);
        }
    });

    if (this.clk_position == position) {
        View view = inflater.inflate(R.layout.activity_mainlist_item_p, null);
        return view;
    }
    return convertView;
}

1 个答案:

答案 0 :(得分:1)

我建议使用单一布局并将其分为两部分。默认情况下,第一部分将可见,第二部分将不可见。当您单击列表项时,使第一部分不可见,第二部分可见。

例如,

<强> list_item_layout.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">

    <!-- replace below TextView with the layout of activity_mainlist_item.xml -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Layout 1"
        android:id="@+id/layout1"
        android:background="@android:color/holo_blue_bright"
        />

    <!-- replace below TextView with the layout activity_mainlist_item_p.xml and don't forget to set android:visibility="gone"  -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Layout 2"
        android:id="@+id/layout2"
        android:visibility="gone"
        android:background="@android:color/holo_purple"
        />
</LinearLayout>

getView()中,请尝试以下

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null || convertView.getTag() == null) {
        convertView = inflater.inflate(R.layout.list_item_layout, parent, false);
        holder = new ViewHolder(convertView);
        // Assuming `ViewHolder` has layout1 and layout2.
        holder.layout1 = (TextView) convertView.findViewById(R.id.layout1);
        holder.layout2 = (TextView) convertView.findViewById(R.id.layout2);
        convertView.setTag(holder);

    }else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.rl_item.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectedItem(position);
            notifyDataSetChanged();
            Intent intent = new Intent(mContext.getApplicationContext(), DetailActivity.class);
            mContext.startActivity(intent);
        }
    });

    if (this.clk_position == position) {
        // Here, set swap the visibility or vice versa depending on your conditions
        holder.layout1.setVisibility(View.GONE);
        holder.layout2.setVisibility(View.VISIBLE);
        return view;
    }
    return convertView;
}