如何在Android中使用simpleAdapter生成的ListView中单击行时显示包含按钮的布局?

时间:2014-04-30 07:52:39

标签: android android-layout android-listview simpleadapter

我使用ListView生成了SimpleAdapter。我在父RelativeLayouts内有2个孩子LinearLayou。我最初使用id多彩不可见的布局。

我想在点击listview行时显示此布局。我已经完成了以下编码,但问题在于,无论我点击哪个行,多彩的布局都只在第一行显示,而不是在点击的行上。

谁能指导我一步一步做什么?我的代码如下:

senttask.java

public class senttask extends Fragment {

    public senttask() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sentaskfragment, container,
                false);
        String font="font/Dosis-SemiBold.otf";
        //String font2="font/Dosis-SemiBold.otf";
        final Typeface tp=Typeface.createFromAsset(getActivity().getAssets(), font);
ListView sent=(ListView)view.findViewById(R.id.listViewtasksent);
  final Databasehandler2 db=new Databasehandler2(getActivity().getApplicationContext());
  HashMap<String, String> c=db.getAddedContacts();

  String name=c.get("ph");
 ArrayList<HashMap<String, String>>val=new ArrayList<HashMap<String,String>>();
 val=db.getTaskSent(name);

  ListAdapter k=new SimpleAdapter(getActivity(),val,R.layout.senttaskdata2,new String[]{"rname","heading","desc","id","path","receiver","sender"},new int[]{R.id.textView1,R.id.textView2,R.id.textView3,R.id.hide1,R.id.hide2,R.id.hide3,R.id.hide4})
  {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final View v = super.getView(position, convertView, parent);
        TextView myname=(TextView)v.findViewById(R.id.textView1);
        TextView mydes=(TextView)v.findViewById(R.id.textView2);
        TextView mytopic=(TextView)v.findViewById(R.id.textView3);


        myname.setTypeface(tp);
        mydes.setTypeface(tp);
        mytopic.setTypeface(tp);

        return v;
    }

  };
  sent.setAdapter(k);

  sent.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

        RelativeLayout r=(RelativeLayout)arg0.findViewById(R.id.colorful);
        r.setVisibility(arg0.VISIBLE);


    }
});
        return view;

    }
}

senttaskdata2.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" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="70dp"

    android:background="@android:color/transparent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:src="@drawable/test2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:text="TextView"
        android:textColor="@android:color/darker_gray"
        android:textSize="11sp" />

    <TextView
        android:id="@+id/hide1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:visibility="gone" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_toRightOf="@+id/imageView1"
        android:text="TextView"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_toRightOf="@+id/imageView1"
        android:text="TextView"
        android:textColor="@android:color/darker_gray"
        android:textSize="11sp" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="70dp" 
    android:visibility="gone"
    android:id="@+id/colorful"
    >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="80dp"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@color/dblue"
        android:src="@drawable/ic_status" />

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="80dp"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@color/dblue"
        android:src="@drawable/ic_status" />

    <ImageView
        android:id="@+id/ImageView02"
        android:layout_width="80dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView2"
        android:background="@color/yellow"
         android:adjustViewBounds="true"
          android:scaleType="centerInside"
        android:src="@drawable/ic_reminder" />

    <ImageView
        android:id="@+id/ImageView03"
        android:layout_width="80dp"
        android:layout_height="70dp"
         android:adjustViewBounds="true"
        android:layout_alignParentTop="true"
         android:scaleType="centerInside"
        android:layout_toRightOf="@+id/ImageView02"
        android:background="@color/green"
        android:src="@drawable/ic_action_edit" />

    <ImageView
        android:id="@+id/ImageView04"
        android:layout_width="80dp"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@color/dblue"
         android:adjustViewBounds="true"
          android:scaleType="centerInside"
        android:src="@drawable/ic_status" />

    <ImageView
        android:id="@+id/ImageView05"
        android:layout_width="80dp"
        android:layout_height="70dp"
         android:scaleType="centerInside"
         android:adjustViewBounds="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@color/red"
        android:src="@drawable/del" />

</RelativeLayout>

senttaskfragment.xml

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

<ListView
    android:id="@+id/listViewtasksent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    >
</ListView>

1 个答案:

答案 0 :(得分:1)

行:

RelativeLayout r=(RelativeLayout)arg0.findViewById(R.id.colorful);
r.setVisibility(arg0.VISIBLE);

将始终引用RelativeLayout的第一个可见行中的ListView,因为arg0代表ListView(所有行最终都是相同的id,因此将返回具有该id的视图的第一次出现)。您不需要在整个RelativeLayout中查找目标ListView,而是需要在实际点击的行视图中查找它,该视图也会将该回调作为arg1参数传递。这样你就可以:

RelativeLayout r=(RelativeLayout)arg1.findViewById(R.id.colorful);
r.setVisibility(arg0.VISIBLE);