java - Android:AdapterView 选择项
问题描述
我想选择我点击的项目,像这样: 有没有可能的方法可以做到这一点?
这就是我构建适配器的方式
adapter = new ArrayAdapter<String>(
context,
android.R.layout.simple_selectable_list_item,
QuestionActivity.OptionArrayList.get(position));
context.startActivity(i);
解决方案
你可以查看我刚刚制作的这个示例项目。我认为代码不言自明,但如果您不确定某些事情,请随时提出问题。但基本上我设置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>
结果(点击“二”后):
推荐阅读
- ios - iOS 13 之后的 GET 请求中的 httpBody
- javascript - 如何在图像加载时显示微调器或进度条?
- python - 调用 Grandfather 类方法导致 RecursionError(超出最大递归深度)
- javascript - 以角度缓存对简单服务的调用
- flutter - 具有嵌套消费者的特定实例的 ChangeNotifierProvider
- vb.net - Visual Studio 2017 VB.NET 错误 BC30451:未声明“变量”。由于其保护级别,它可能无法访问
- shopify - 在 shopify 中想要在我的下拉列表中获取产品的不同颜色名称
- node.js - 将 Socket IO 与节点 js socket.to(socket.id).emit() 一起使用会引发错误
- wso2 - wso2-apim 发布者在加载 api 时卡住
- excel - 日期在图表上显示为 1900 年