我不知道在旋转列表时是否有任何错误或需要做其他事情。如果有人能帮助我找出解决方案,我会非常感激。
我有一个垂直ListView
,有5个项目(在屏幕截图的左侧),如屏幕截图所示:
正如您所看到的,每次单击或选择项目时都应该旋转列表。问题是我实现的代码与onItemClickListener
一起使用(当单击的项目位于项目下方或上方的一个位置时中间),但它不适用onItemSelectedListener
(列表旋转两次(单向),然后停止向该方向旋转,这意味着onItemSelected
根本不被调用。
好吧,这可能会让你更加困惑..我想要一个列表,其中所选项目采用中间(文本颜色蓝色)位置,列表相应地旋转。 请帮助....非常感谢。
源代码:主要活动:
public class MainActivity extends AppCompatActivity {
private ListView categoryList;
ListCustomAdapter listadapter;
private List<TextItem> txtItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categoryList = (ListView) findViewById(R.id.listCategories);
listadapter=new ListCustomAdapter(this,txtItems);
categoryList.setAdapter(listadapter);
categoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
listadapter.getPosition(i);
Log.d("position",""+i);
if(i>2) {
Log.d("position1",""+(i-1));
Collections.rotate(txtItems, 4);
listadapter.notifyDataSetChanged();
}else if(i<2){
Log.d("position2",""+(1-i));
Collections.rotate( txtItems,1);
listadapter.notifyDataSetChanged();
}
else{
}
}
});
categoryList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("inside" ,"onItemSelected");
if(i>2) {
Collections.rotate(txtItems,4);
Log.d("position rotated",""+i);
listadapter.notifyDataSetChanged();
}else if(i<2){
Collections.rotate( txtItems,1);
listadapter.notifyDataSetChanged();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
Log.e("inside" ,"onNothingSelected");
}
});
}
ListCustomAdapter:
public class ListCustomAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<TextItem> txtItems;
int pos;
public ListCustomAdapter(Activity activity, List<TextItem> txtItems) {
this.activity = activity;
this.txtItems = txtItems;
}
@Override
public int getCount() {
return txtItems.size();
}
@Override
public Object getItem(int location) {
return txtItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
/*
inflate the items in the list view
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (inflater == null) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = inflater.inflate(R.layout.vertical_list_item, null);
holder = new ViewHolder();
holder.category = (TextView) convertView.findViewById(R.id.category_name);
holder.listLayout= (LinearLayout) convertView.findViewById(R.id.list_layout);
convertView.setTag(holder);
}else
{
holder = (ViewHolder) convertView.getTag();
}
if (pos == position) {
Log.d("translated","done of position"+pos);
holder.category.setTranslationX(10.17f);
} else {
/* view.imgViewFlag.getLayoutParams().height = 140;
view.imgViewFlag.getLayoutParams().width = 300;*/
holder.category.setTranslationX(1.0f);
Log.d("translated","default of position"+pos);
// view.imgViewFlag.setBackgroundResource(0);
holder.category.setBackgroundResource(0);
}
TextItem model = txtItems.get(position);
holder.category.setText(model.getTitleName());
if(position==2)
holder.category.setTextColor(Color.BLUE);
/*
creating objects to access the views
*/
return convertView;
}
public void getPosition(int position) {
this.pos = position;
}
static class ViewHolder {
TextView category;
LinearLayout listLayout;
}
}
答案 0 :(得分:0)
由于我的代码适用于OnItemClickListener,我发现点击的项目在列表中设置为选中..对于OnItemSelectedListener ...我必须始终将中间位置设置为选中..因此,我添加了以下
categoryList.setSelection(2); /*2 is the mid position for the list with 5 items *\
列表旋转后,即:
categoryList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("inside" ,"onItemSelected");
if(i>2) {
Collections.rotate(txtItems,4);
categoryList.setSelection(2);
Log.d("position rotated",""+i);
listadapter.notifyDataSetChanged();
}else if(i<2){
Collections.rotate( txtItems,1);
categoryList.setSelection(2);
listadapter.notifyDataSetChanged();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
Log.e("inside" ,"onNothingSelected");
}
});