我有一个ListView,用于一次显示一个问题列表。我在列表项中使用了5个ImageView,即每个列表项(问题)有5个答案。 我需要的是,当我点击任何图像时,它应该首先显示所选图像的黑色背景颜色,比如说500毫秒,然后它应该移动到下一个问题。 目前,当我点击任何列表项时,它会立即转到下一个问题而不显示黑色背景。
que.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lyot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv1"
android:layout_width="75dp"
android:layout_height="78dp"
android:background="@drawable/selector_iv1"
android:clickable="true"
android:src="@drawable/sa" />
<ImageView
android:id="@+id/iv2"
android:layout_width="75dp"
android:layout_height="78dp"
android:src="@drawable/sb" />
<ImageView
android:id="@+id/iv3"
android:layout_width="75dp"
android:layout_height="78dp"
android:src="@drawable/sc" />
<ImageView
android:id="@+id/iv4"
android:layout_width="75dp"
android:layout_height="78dp"
android:src="@drawable/sd" />
<ImageView
android:id="@+id/iv5"
android:layout_width="75dp"
android:layout_height="78dp"
android:src="@drawable/se" />
</LinearLayout>
layout.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="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/queList"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_marginBottom="25dp"
android:divider="@null"
android:focusableInTouchMode="false"
android:overScrollMode="never"
android:scrollbars="none" />
</LinearLayout>
Activity.java
public class Activity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
ListView queList = (ListView) findViewById(R.id.queList);
refreshQuestionList();
}
private void refreshQuestionList()
{
queList.setAdapter(new quesAnsListAdapter(context));
queList.setSelection(currentQuestionPosition);
}
private class quesAnsListAdapter extends BaseAdapter
{
private Context mContext;
public quesAnsListAdapter(Context context)
{
mContext = context;
}
public int getCount()
{
return 10;
}
public Object getItem(int position)
{
return myArrayListItem;
}
public long getItemId(int position)
{
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder = new ViewHolder();
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(R.layout.que, null);
holder.iv1 = (ImageView) row.findViewById(R.id.iv1);
holder.iv2 = (ImageView) row.findViewById(R.id.iv2);
holder.iv3 = (ImageView) row.findViewById(R.id.iv3);
holder.iv4 = (ImageView) row.findViewById(R.id.iv4);
holder.iv5 = (ImageView) row.findViewById(R.id.iv5);
row.setTag(holder);
}
else
{
holder = (ViewHolder) row.getTag();
}
holder.que.setText("Que"+position);
for (int i = 0; i < ansBeanList.size(); i++)
{
final AnsBean ab = ansBeanList.get(i);
switch (i)
{
case 0:
holder.iv1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
v.setBackgroundColor(Color.BLACK);
smilyClick(position);
}
});
break;
case 1:
holder.iv2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
v.setBackgroundColor(Color.BLACK);
smilyClick(position);
}
});
break;
case 2:
holder.iv3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
v.setBackgroundColor(Color.BLACK);
smilyClick(position);
}
});
break;
case 3:
holder.iv4.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
v.setBackgroundColor(Color.BLACK);
smilyClick(position);
}
});
break;
case 4:
holder.iv5.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
v.setBackgroundColor(Color.BLACK);
smilyClick(position);
}
});
break;
default:
break;
}
}
return row;
}
class ViewHolder
{
ImageView iv1, iv2, iv3, iv4, iv5;
}
}
private int currentQuestionPosition=0,finalQuestionPosition=9;
private void smilyClick(int oldQuestionPosition)
{
boolean answerInsertedOrUpdated = false;
if (oldQuestionPosition < finalQuestionPosition)
currentQuestionPosition = (oldQuestionPosition + 1);
if (oldQuestionPosition < finalQuestionPosition)
{
refreshQuestionList();
}
}
}
答案 0 :(得分:0)
启动应用时currentQuestionPosition=0,finalQuestionPosition=9;
在smily中点击你做的refreshQuestionList()
根据之前说明的值调用。
但是这个方法删除了适配器并创建了新的,这导致整个ListView重绘。