处理大量可点击的行

Handling large number of clickable rows

我正在开发一个 Android 项目,该项目有很多屏幕,每个屏幕都可以点击超过 200 行。我想弄清楚的问题是如何在不添加 200 行

的情况下使它们都能够被点击
TableRow r1 = (TableRow) findViewById(R.id.table_row_1);
TableRow r2 = (TableRow) findViewById(R.id.table_row_2);
TableRow r3 = (TableRow) findViewById(R.id.table_row_3);
TableRow r4 = (TableRow) findViewById(R.id.table_row_4);

r1.setOnClickListener(listener);
r2.setOnClickListener(listener);
r3.setOnClickListener(listener);
r4.setOnClickListener(listener);

最终,这些行将采用它们的 ID 并在数据库中搜索值(我将使用每个 table 行作为数据库中值的键来填充行中的列)但现在我只是尝试在单击每个行时更改行的背景颜色。

问题: 如何在没有数千行冗余代码的情况下处理大量可点击的行?我是否需要为每一行设置一个 OnClickListener 或是否有更好的方法我正在寻找?有没有办法在 XML 中做到这一点?

使用 ListView 和自定义 ListAdapter

的解决方案

MainActivity.java:

public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpComponents();
    }

    private void setUpComponents(){
        ArrayList<String> myValuesToDisplay = getDatabaseContent();
        MyCustomListAdapter adapter = new MyCustomListAdapter(this, myValuesToDisplay);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }

    private ArrayList<String> getDatabaseContent(){
        /*
        This is where you would like to connect to your database and fetch the content.
        In this example, we simulate the result by returning an ArrayList<String>
         */

        ArrayList<String> values = new ArrayList<String>();
        values.add("Value1");
        values.add("Value2");
        values.add("Value3");
        return values;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //When you click on an item in the list view, you fetch the position in the list
        System.out.println("Clicked on item with position: " + position);
    }

}

MyCustomListAdapter.java:

public class MyCustomListAdapter extends ArrayAdapter<String> {

    private ArrayList<String> yourArray;

    public MyCustomListAdapter(Context ctx, ArrayList<String> yourArray){
        super(ctx, R.layout.my_custom_list_item, yourArray);
        this.yourArray = yourArray;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Re-use rows to save battery
        View row;
        if (convertView == null) {
            //We inflate our custom view for the ListView item
            LayoutInflater inflater = LayoutInflater.from(getContext());
            row = inflater.inflate(
                    R.layout.my_custom_list_item, null);
        } else {
            row = convertView;
        }

        //Get the current item of the array
        String arrayItem = yourArray.get(position);
        //Get the text view in the layout of which we want to display the value
        TextView tvListItem = (TextView) row.findViewById(R.id.tv_list_item);
        //Set the text
        tvListItem.setText(arrayItem);
        //Return the row to the ListView
        return row;
    }
}

ActivityMain.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/list" />
</RelativeLayout>

my_custom_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/tv_list_item" />
</LinearLayout>

此解决方案将创建一个可滚动的 ListView 并用您的数据库值填充它。您对 ListAdapter 的实施可能会有所不同。您可以通过更改 my_custom_list_item.xml.

中的布局来选择要显示的内容和方式

结果:

点击一行将打印出它在列表中的位置。例如,您可以使用该信息启动另一个 activity 显示有关该条目的详细信息。