Junior Frogie
Junior Frogie

Reputation: 313

How to click on listview item after change it to different to layout?

When I click on listview, it will change that item to different layout. But after that I can't click on that item anymore. What am I supposed to do? Please help me. Thank you.

@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;
}

Upvotes: 1

Views: 977

Answers (1)

Faraz
Faraz

Reputation: 2154

I suggest using single layout and divide it into two sections. First section will be visible by default and second section will be invisible. When you click on list item, make first section invisible and second section visible.

For example,

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>

In getView(), try as below

@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;
}

Upvotes: 1

Related Questions