首页 > 解决方案 > Android:AdapterView 选择项

问题描述

我有一个装满物品的 AdapterView在此处输入图像描述

我想选择我点击的项目,像这样:在此处输入图像描述 有没有可能的方法可以做到这一点?

这就是我构建适配器的方式

adapter = new ArrayAdapter<String>(
                            context,
                            android.R.layout.simple_selectable_list_item,
                            QuestionActivity.OptionArrayList.get(position));
                    context.startActivity(i);

标签: javaandroid

解决方案


你可以查看我刚刚制作的这个示例项目。我认为代码不言自明,但如果您不确定某些事情,请随时提出问题。但基本上我设置OnItemClickListener为 ListView 并将背景更改为selected_back可绘制文件夹中的形状。此外,我保留了对上次更改视图的引用,因此当用户单击另一个视图时,很容易将其背景再次更改为normal_back.

MainActivity.java

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity
{
    View lastChangedView = null;

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

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("One");
        arrayList.add("Two");
        arrayList.add("Three");

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                R.layout.list_view_item,
                arrayList);

        ListView listView = findViewById(R.id.listView);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                if (lastChangedView != null)
                {
                    lastChangedView.setBackgroundResource(R.drawable.normal_back);
                }
                lastChangedView = view;
                Log.d("Main", "Item click at " + position);
                view.setBackgroundResource(R.drawable.selected_back);
            }
        });
    }
}

aactivity_main.xml

<FrameLayout 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"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

list_view_item.xml(在布局文件夹中)

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="32sp"
    android:background="@drawable/normal_back" />

normal_back.xml(在可绘制文件夹中)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF" />
</shape>

selected_back.xml(在可绘制文件夹中)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <stroke
        android:width="2dp"
        android:color="#0000FF" />
    <solid android:color="#FFFFFF" />
</shape>

结果(点击“二”后):

在此处输入图像描述


推荐阅读