我在屏幕上分割了两个ListView。我想实现以下目标: 单击顶部列表项可更改其背景颜色,以及底部列表中相同位置的项目的背景颜色。
我有以下问题: 底部列表项的背景仅在我将其从屏幕上滚动并将其滚动回屏幕后才会更改。如何立即更改颜色?
另外,如何更改单个项目的背景颜色?现在,当我点击一个项目时,它会更改它的颜色,当我点击另一个列表项时,前一项保持颜色。我希望一次有一个彩色列表项。我该怎么做?
这是我的代码......
MainActivity.java:
public class MainActivity extends AppCompatActivity {
ListView listViewTop, listViewBottom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listViewTop = (ListView) findViewById(R.id.list1);
listViewBottom = (ListView) findViewById(R.id.list2);
CustomAdapter customAdapter = new CustomAdapter(this, addUsersToList());
listViewTop.setAdapter(customAdapter);
listViewBottom.setAdapter(customAdapter);
}
private ArrayList<Model> addUsersToList() {
ArrayList<Model> modelList = new ArrayList<>();
modelList.add(new Model("John"));
modelList.add(new Model("Mary"));
modelList.add(new Model("Bob"));
modelList.add(new Model("Tom"));
modelList.add(new Model("Danny"));
modelList.add(new Model("Mike"));
modelList.add(new Model("Ron"));
modelList.add(new Model("Harry"));
modelList.add(new Model("Dan"));
return modelList;
}
}
CustomAdapter.java:
public class CustomAdapter extends ArrayAdapter<Model> {
private static class ViewHolder {
TextView userTextView;
}
public CustomAdapter(Context context, ArrayList<Model> usersArray) {
super(context, 0, usersArray);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Model currentUser = getItem(position);
View userRowView = convertView;
final ViewHolder viewHolder;
if (userRowView == null) {
viewHolder = new ViewHolder();
userRowView = LayoutInflater.from(getContext()).inflate(
R.layout.user_layout, parent, false);
viewHolder.userTextView = (TextView) userRowView.findViewById(R.id.userTextView);
final View finalUserRowView = userRowView;
viewHolder.userTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentUser.setClicked(true);
finalUserRowView.setBackgroundColor(Color.YELLOW);
}
});
userRowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) userRowView.getTag();
}
viewHolder.userTextView.setText(currentUser.getUserName());
if (currentUser.isClicked())
userRowView.setBackgroundColor(Color.YELLOW);
else
userRowView.setBackgroundColor(Color.WHITE);
return userRowView;
}
}
Model.java:
public class Model {
private String userName;
private boolean isClicked;
public Model(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public boolean isClicked() {
return isClicked;
}
public void setClicked(boolean clicked) {
isClicked = clicked;
}
}
编辑:解决了我的问题,感谢您的帮助。这是我的适配器:
private static Model mSelectedUser = null;
private static class ViewHolder {
TextView userTextView;
}
public CustomAdapter(Context context, ArrayList<Model> usersArray) {
super(context, 0, usersArray);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Model currentUser = getItem(position);
View userRowView = convertView;
final ViewHolder viewHolder;
if (userRowView == null) {
viewHolder = new ViewHolder();
userRowView = LayoutInflater.from(getContext()).inflate(
R.layout.user_layout, parent, false);
viewHolder.userTextView = (TextView) userRowView.findViewById(R.id.userTextView);
userRowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) userRowView.getTag();
}
viewHolder.userTextView.setText(currentUser.getUserName());
userRowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mSelectedUser != null)
mSelectedUser.setClicked(false);
mSelectedUser = currentUser;
mSelectedUser.setClicked(true);
notifyDataSetChanged();
}
});
if (currentUser.isClicked()) {
userRowView.setBackgroundColor(Color.YELLOW);
((ListView) parent).setSelection(position);
parent.requestFocus();
}
else
userRowView.setBackgroundColor(Color.TRANSPARENT);
return userRowView;
}
答案 0 :(得分:0)
你应该去布局和android:background =“”在这里添加自定义颜色 像这样:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"/>
答案 1 :(得分:0)
对于最后一个问题,您必须将以前的彩色项目的颜色更改为默认颜色,或者您可以将所有项目颜色更改为默认值,然后更改所选的项目颜色。
对于另一个问题,你可以使用它:
myListView.invalidateViews();
答案 2 :(得分:0)
我希望它会对你有所帮助 您可以使用选择器。更改颜色值并根据需要修改以下内容。
可绘制文件夹中的backgroundlistview.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/pressed" />
<item android:state_focused="false"
android:drawable="@drawable/normal" />
</selector>
drawable文件夹中的pressed.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FF1A47"/> // color
<stroke android:width="3dp"
android:color="#0FECFF"/> // border
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners android:bottomRightRadius="7dp" // for rounded corners
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
drawable文件夹中的normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="3dp"
android:color="#0FECFF" />
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
将背景绘制设置为listview自定义布局,以便为每行充气
我建议使用自定义列表视图和自定义适配器。
android:background="@drawable/backgroundlistview"
如果您没有使用自定义适配器,可以将listselector设置为listview,如下所示
android:listSelector="@drawable/backgroundlistview"
答案 3 :(得分:0)
试试这个:在你的活动中
.btn
答案 4 :(得分:0)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.prgmaker.background_color_list.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List1"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="#0000ff" />
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:choiceMode="singleChoice" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List2"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="#0000ff" />
<ListView
android:id="@+id/list2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:choiceMode="singleChoice" />
</LinearLayout>
public class CheckableLinearLayout extends LinearLayout implements Checkable {
private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
private boolean mChecked = false;
public CheckableLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean isChecked() {
return mChecked;
}
public void setChecked(boolean b) {
if (b != mChecked) {
mChecked = b;
refreshDrawableState();
}
}
public void toggle() {
setChecked(!mChecked);
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/holo_orange_light" android:state_checked="true" />
<item android:drawable="@android:color/white" />
</selector>
<com.prgmaker.background_color_list.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_item_background"
android:gravity="center_vertical"
android:minHeight="?android:listPreferredItemHeight"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<TextView
android:id="@+id/userTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingStart="15dp"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@color/colorAccent" />
</com.prgmaker.background_color_list.CheckableLinearLayout>
public class CustomAdapter extends ArrayAdapter<Model> {
public CustomAdapter(Context context, ArrayList<Model> usersArray) {
super(context, 0, usersArray);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Model currentUser = getItem(position);
View userRowView = convertView;
final ViewHolder viewHolder;
if (userRowView == null) {
viewHolder = new ViewHolder();
userRowView = LayoutInflater.from(getContext()).inflate(
R.layout.user_layout, parent, false);
viewHolder.userTextView = (TextView) userRowView.findViewById(R.id.userTextView);
userRowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) userRowView.getTag();
}
viewHolder.userTextView.setText(currentUser.getUserName());
return userRowView;
}
private static class ViewHolder {
TextView userTextView;
}
}
listViewTop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listViewBottom.setItemChecked(position, true);
}
});
答案 5 :(得分:0)
您只需要更换适配器。
public class CustomAdapter extends ArrayAdapter<Model> {
private static class ViewHolder {
TextView userTextView;
}
public CustomAdapter(Context context, ArrayList<Model> usersArray) {
super(context, 0, usersArray);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Model currentUser = getItem(position);
View userRowView = convertView;
final ViewHolder viewHolder;
if (userRowView == null) {
viewHolder = new ViewHolder();
userRowView = LayoutInflater.from(getContext()).inflate(
R.layout.user_layout, parent, false);
viewHolder.userTextView = (TextView) userRowView.findViewById(R.id.userTextView);
final View finalUserRowView = userRowView;
viewHolder.userTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Object posTag = v.getTag("pos");
if(pasTag != null) {
int pos = (int)posTag;
Model model = getItem(pos);
model.setClicked(true);
}
}});
userRowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) userRowView.getTag();
}
viewHolder.userTextView.setText(currentUser.getUserName());
viewHolder.userTextView.setTag("pos",position);
if (currentUser.isClicked())
userRowView.setBackgroundColor(Color.YELLOW);
else
userRowView.setBackgroundColor(Color.WHITE);
return userRowView;
}
}