如何在listview外部的按钮中设置listview行内任何视图的可见性

时间:2017-03-07 04:43:20

标签: android

我在工具栏上有编辑按钮,单击此按钮我想在列表视图的每一行内显示新视图,即十字按钮。这是可能的,如果是的话怎么样?提前致谢。下面是布局文件:

主要布局:

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

    <LinearLayout
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:background="@color/TabColor"
        android:weightSum="10"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_weight="1"
            android:onClick="back2"
            android:layout_gravity="center"
            app:srcCompat="@drawable/back_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_weight="7"
            android:paddingLeft="@dimen/margin_small"
            android:text="List OF Hotels"
            android:textSize="@dimen/text_title"
            android:textColor="@color/WhiteColor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <FrameLayout
            android:layout_weight="2"
            android:background="@drawable/transparent_bg"
            android:layout_marginRight="@dimen/margin_large"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/edit_jobs"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_marginBottom="@dimen/margin_small"
                    android:layout_marginTop="@dimen/margin_small"
                    android:text="Edit"
                    android:textColor="@color/WhiteColor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

            <LinearLayout
                android:visibility="invisible"
                android:id="@+id/done_editing"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_marginBottom="@dimen/margin_small"
                    android:layout_marginTop="@dimen/margin_small"
                        android:text="Done"
                    android:textColor="@color/WhiteColor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </FrameLayout>

    </LinearLayout>

    <ListView
        android:id="@+id/listview1"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

listview行布局,我必须显示交叉图像:

<?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:layout_marginTop="@dimen/margin_large"
        android:layout_marginLeft="@dimen/margin_large"
        android:layout_marginRight="@dimen/margin_large"
        android:background="@drawable/border"
        android:padding="@dimen/margin_large"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

<LinearLayout
    android:weightSum="10"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:text=""
        android:layout_gravity="center"
        android:layout_weight="9"
        android:textColor="@color/TabColor"
        android:singleLine="true"
        android:textSize="@dimen/text_large"
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:gravity="right"
            android:id="@+id/arrow"
            android:src="@drawable/rightarrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:visibility="invisible"
            android:id="@+id/cross"
            android:gravity="right"
           android:src="@drawable/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </FrameLayout>

</LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:weightSum="10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:text=""
                android:layout_weight="5"
                android:textColor="@color/blackColor"
                android:textSize="@dimen/text_medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_weight="5"
                android:text=""
                android:gravity="right"
                android:textColor="@color/blackColor"
                android:textSize="@dimen/text_medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

</LinearLayout>
    </LinearLayout>

适配器类:

public class MyAdapter extends ArrayAdapter<String> {

    private final Context context;
    private ArrayList<String> titlelist,datalist;

    public MyAdapter(Context context, ArrayList<String> title, ArrayList<String> data) {
        super(context, R.layout.listview_row, title);
        titlelist= new ArrayList<>();
        datalist= new ArrayList<>();
        this.context=context;
        this.titlelist=title;
        this.datalist=data;
    }

    private static class ViewHolder {
        TextView t1,t2;
    }

    @Override
    public int getCount() {
        return titlelist.size();
    }
    @Override
    public String getItem(int position) {
        return titlelist.get(position);
    }

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

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.listview_row, null);

            holder = new ViewHolder();
            holder.t1 = (TextView) convertView.findViewById(R.id.title);
            holder.t2 = (TextView) convertView.findViewById(R.id.data);
            convertView.setTag(holder);

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

        holder.t1.setText(titlelist.get(position));
        holder.t2.setText(datalist.get(position));

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:2)

您必须在适配器中添加此功能,

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    //ur other codes.....
    //
    //
    if(crossButtonPress){
        holder.imageView.setImageResource(R.drawable.cross);
    }else{
        holder.imageView.setImageResource(R.drawable.default_ic);
    }
}

另外,在你实际按钮的onClick上,

crossButtonPress = true;
adapter.notifyDatasetChanged();