2

我正在显示一个自定义列表视图,我想从另一个按钮遍历列表,一个用于向下列表,一个用于向上列表,所以在这里我能够获取当前选定的项目位置,所以我正在使用 -

list.setSelection(newpostion);

但我无法看到给定位置的亮点。在这里,列表正在被正确遍历,但用户无法看到他当前在哪个列表项上。所以请告诉我如何突出显示或更改特定列表项的背景。

我尝试使用选择器,例如状态更改背景,但不适用于我。请为我提供任何可行的解决方案。

这是列表适配器的代码-

package com.sho;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class list_adapter extends ArrayAdapter<HashMap<String,String>>
{
ArrayList<HashMap<String,String>> items;
LayoutInflater mInflater ; 
Context context;
int layoutResourceId; 

public list_adapter(Context context, int layoutResourceId, ArrayList<HashMap<String, String>> _list) 
{
        super(context, layoutResourceId, _list);
        this.layoutResourceId=layoutResourceId;
        this.items = _list;
        this.context=context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
            View row = convertView;
            ViewHolder holder = new ViewHolder();
            if(row==null)
            {
                mInflater = ((Activity)context).getLayoutInflater();
                row = mInflater.inflate(R.layout.chapterdetails, parent, false);
                holder.name = (TextView) row.findViewById(R.id.chapterdata);


            }

            else
            {
                holder=(ViewHolder)row.getTag();

            }

            row.setTag(holder);
            row.setId(position);

       HashMap<String,String>  map=items.get(position);
       holder.name.setText(map.get("DESCRIPTION"));
        return row;
}

static class ViewHolder
{
    TextView name;
    ImageButton annotion,bookmark,edit;
}

}

我想要遍历列表的按钮的代码,这里的列表名称是 ls-

View earliest=new View(MainActivity.this);

                if(postion<a1&&postion<=a1-2) 
                {
                     postion++;
                     earliest=ls.getRootView().findViewById(postion);
                        earliest.setBackground(null);




                     View v=new View(MainActivity.this); 
                    v=ls.getRootView().findViewById(postion); 
                    v.setBackgroundColor(Color.RED);
                    previous.setBackgroundColor(Color.BLUE); // your list
                    previous=v;
                     } } }); 
4

2 回答 2

0

它可能与可聚焦性有关。如果您有一个自定义列表视图,其中包含可聚焦对象,请尝试使它们不可聚焦。

aButtonInYourListView.Focusable = false;
aButtonInYourListView.FocusableInTouchMode = false;

对 ListViewAdapter 中的所有按钮、图像按钮执行此操作。根据我的经验,它也可以从 xml 完成,但不能用于 imagebutton。为了安全起见,在代码中执行此操作。

这应该允许 ListViewItems 成为焦点。

于 2013-03-04T10:52:12.960 回答
0

编辑:设置后list.setSelection(newpostion);

得到这样的看法

View v=list.getSelectedView();

然后遵循相同的逻辑

你的代码是这样的

创建全局变量

View previous;

在 onCreate 中初始化它

previous=new View(this);



 traverse_listdown.setOnClickListener(new OnClickListener() {
 @Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
System.out.println("down"); 
int a1=list.getCount();

if(postion<a1&&postion<=a1-2) {
 postion++; list.setSelection(postion);
 View v1=new View(MainActivity.this); 
v1=list.getSelectedView(); 
v1.setBackgroundColor(Color.RED);
previous.setBackgroundColor(Color.WHITE); // your list
previous=v1;
 } } }); 

试试这个,

像这样创建一个可绘制的选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/pause_button"
          android:state_selected="true" />
    <item android:drawable="@drawable/play_button" />
</selector>

您可以指定您选择的颜色或可绘制对象。

像这样创建全局变量

View previous;

初始化它

previious=new View(context);

当您的列表项单击时,像这样使用

yourlist.setOnItemClickListener(new OnItemClickListener() {
            @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3) {
        previous.setSelected(false);                        
        view.setSelected(true);
        previous=view;

}
});
于 2013-03-04T11:19:55.467 回答