Android的自定义列表视图行布局问题 [英] Android custom list view row layout issue

查看:116
本文介绍了Android的自定义列表视图行布局问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义如下,其中包含的TextView,CheckBox和进度条的布局。我有两个问题: -

I have defined following layout which contains TextView, CheckBox and ProgressBar. I have two questions:-


  1. 当我点击列表项,它自动从升序到降序,反之亦然,什么可能是错误的,我实现排序?

  1. When I clicked on list item, it automatically get sorted from Ascending to descending and vice versa, what might be wrong in my implementation?

如果我不停复选框的知名度为true,那么我没有得到onListItemClick()事件。

If I kept the visibility of Checkbox to true then I am not getting the onListItemClick() event.

请找到下面的布局,我是新手到Android。

Please find layout below, I am newbie to android.

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/textUrl"
    android:layout_width="318dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="40dp"
    android:text="TextView" />

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textUrl"
    android:layout_marginLeft="40dp" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="CheckBox" />

下面是我使用自定义的适配器类。

Below is custom adapter class that I am using.

 public class CustomListViewAdapter extends ArrayAdapter<ListItemData>
{
    Context mContext;
    int mlayoutResourceId;
List<ListItemData> mlistData = null;

public CustomListViewAdapter(Context context,List<ListItemData> objects) 
{
    super(context, R.layout.custom_listview_row2, objects);

    mContext = context;
    mlistData = objects;
}       

@SuppressWarnings("static-access")
@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View v = convertView;
    ViewHolder vh;
    if (null == v)
    {
        LayoutInflater inflator = ((Activity)mContext).getLayoutInflater();
        v = inflator.inflate(R.layout.custom_listview_row2, parent,false);
        vh = new ViewHolder();
        vh.mUrlTextView = (TextView) v.findViewById(R.id.textUrl);
        vh.mProgressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
        vh.mCheckBox = (CheckBox)v.findViewById(R.id.checkBox);

        v.setTag(vh);
    }
    else
    {
        vh = (ViewHolder)v.getTag();
    }
    ListItemData itemData = mlistData.get(position);
    vh.mUrlTextView.setText(itemData.mUrlName);
    vh.mProgressBar.setProgress(itemData.mProgressValue);
    return v;
}   

}

code为ListActivity

Code for ListActivity

    public class CustomListViewActivity extends ListActivity 
{
    //List<HashMap<String, Object>> fillList = new ArrayList<HashMap<String, Object>>();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_list_view);
        ArrayList<ListItemData> list = new ArrayList<ListItemData>();

        for (int idx = 0; idx < 10;idx++)
        {
        ListItemData item = new ListItemData("", 0);
        item.mUrlName = String.format("www.url-%d.com", idx);
        item.mProgressValue = idx*10;
        list.add(item);
    }       
    CustomListViewAdapter adapter = new CustomListViewAdapter(this,list);
    setListAdapter(adapter);
}

@Override   
protected void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);
     ListItemData item = (ListItemData)getListAdapter().getItem(position);
     Toast.makeText(this, String.format("%s - %d", item.mUrlName,item.mProgressValue), Toast.LENGTH_LONG).show();
}

}

推荐答案

我已经找到了以下问题的答案

I have found the answer for below question

如果我不停复选框的知名度为true,那么我没有得到的
  onListItemClick()事件。

If I kept the visibility of Checkbox to true then I am not getting the onListItemClick() event.

应答 - TextView的,多选和进度条的可聚焦以及相对布局设置为false。

Answer - Set the focusable of Textview, checkbox and progress bar as well as relative layout to false.

这篇关于Android的自定义列表视图行布局问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆