我有一个使用ListView
生成的SimpleAdapter
。我在xml文件中有一个textview和一个ImageView,它与SimpleAdapter一起使用。首先将imageview设置为不可见。单击Listview中的行时,Imageview变为可见。我想做的是以下几点:
如果我在Listview中有4行,如果我单击第1行,那么ImageView应该在第1行可见,那么如果我单击第2行,则imageview应该从第1行变为不可见,并且应该仅在第2行可见。 / p>
我已完成以下操作,但它的工作方式如下:
如果单击第1行,则在第1行上可以看到ImageView。然后,如果我单击第2行,ImageView将在第2行显示,并且在第1行也可见。我想让它从第1行看不见,只显示在点击的行上。
任何人都可以一步一步指导我如何做到这一点。我的代码和xml如下:
myactivity.class
final ListAdapter k=new SimpleAdapter(getActivity(),val,R.layout.senttaskdata2,new String[]{"rname"},new int[]{R.id.textView1})
{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final View v = super.getView(position, convertView, parent);
ImageView im=(ImageView)v.findViewById(R.id.ImageView3);
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ImageView im=(ImageView)arg1.findViewById(R.id.ImageView3);
setVisibility(arg1.VISIBLE);
});
return v;
}
};
sent.setAdapter(k);
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/imageView3"
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/textView1"
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" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:2)
我有同样的问题,我找到了一种简单的方法。
首先,在您的自定义适配器
中public class MyAdapter extends BaseAdapter {
private int selectedIndex;
private boolean visible;
// omitted code
}
// omitted code
public View getView(int index, View view, ViewGroup parent) {
TextView textView= (TextView) view.findViewById(R.id.text_view);
ImageView imgView = (ImageView) view.findViewById(R.id.image_view);
textView.setText("some text");
if(index == selectedIndex) {
imgView .setVisibility(View.VISIBLE);
} else {
imgView .setVisibility(View.INVISIBLE);
}
return view;
}
其次,在你的活动中
public void onCreate(Bundle bundle) {
// omitted code
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
// change index of selected item
fontAdapter.setSelectedIndex(position);
fontAdapter.notifyDataSetChanged();
}
});
// omitted code
}
答案 1 :(得分:1)
试试这个我实现了自己的适配器,而不是使用简单适配器
<强> ListAdapter.java 强>
package com.stackoverflow.listviewanswer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter
{
private boolean[] mVisibilityList = null;
private String[] mNameList = null;
private Context mContext = null;
private LayoutInflater mInflater = null;
public ListAdapter(Context iContext, boolean[] iVisibilityList, String[] iNameList)
{
mVisibilityList = iVisibilityList;
mContext = iContext;
mInflater = LayoutInflater.from(mContext);
mNameList = iNameList;
}
public void setVisibilityList(boolean[] iVisibilityList)
{
mVisibilityList = iVisibilityList;
}
@Override
public int getCount()
{
return mVisibilityList.length;
}
@Override
public Object getItem(int iPosition)
{
return mVisibilityList[iPosition];
}
@Override
public long getItemId(int iPosition)
{
return iPosition;
}
@Override
public View getView(int iPosition, View iConvertView, ViewGroup iViewGroup)
{
Holder holder = new Holder();
if (iConvertView == null)
{
iConvertView = mInflater.inflate(R.layout.list, null);
holder.mImageView = (ImageView) iConvertView.findViewById(R.id.imageView3);
holder.mTextView = (TextView) iConvertView.findViewById(R.id.textView1);
iConvertView.setTag(holder);
}
else
{
holder = (Holder) iConvertView.getTag();
}
// SET DATA
if (mVisibilityList[iPosition])
{
holder.mImageView.setVisibility(View.VISIBLE);
}
else
{
holder.mImageView.setVisibility(View.INVISIBLE);
// OR
// holder.mImageView.setVisibility(View.GONE);
// Use as per your needs
}
holder.mTextView.setText(mNameList[iPosition]);
return iConvertView;
}
private class Holder
{
TextView mTextView = null;
ImageView mImageView = null;
}
}
<强> MainActivity.java 强>
package com.stackoverflow.listviewanswer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity implements OnItemClickListener
{
ListView mListView = null;
boolean[] mVisisbilityList = { false, false, false, false };
String[] mNameList = { "TextView 1", "TextView 2", "TextView 3", "TextView 4" };
ListAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.list_view);
mListView.setOnItemClickListener(this);
adapter = new ListAdapter(this, mVisisbilityList, mNameList);
mListView.setAdapter(adapter);
}
@Override
public void onItemClick(AdapterView<?> iAdapterView, View iView, int iPosition, long iId)
{
for (int i = 0; i < 4; i++)
{
mVisisbilityList[i] = false;
}
mVisisbilityList[iPosition] = true;
adapter.setVisibilityList(mVisisbilityList);
adapter.notifyDataSetChanged();
}
}
list.xml 与您的senttaskdata2.xml相同