我正在为平板电脑开发移动应用程序,并使用Android Studio步骤添加了主数据/详细信息流。我注意到,当您选择一个项目时,该项目未突出显示,用户无法确定他触摸了哪个项目。
我进行了研究,但没有发现对我有用的东西。所有建议均针对ListView,我的案例使用RecyclerView 。
我尝试过至少在onClick方法中更改背景颜色,并且该方法有效,但是当您触摸其他项目时,颜色不会消失。
ProductListActivity:
public class ProductListActivity extends AppCompatActivity {
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(getTitle());
if (findViewById(R.id.product_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-w900dp).
// If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
}
View recyclerView = findViewById(R.id.product_list);
assert recyclerView != null;
setupRecyclerView((RecyclerView) recyclerView);
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(this, DummyContent.ITEMS, mTwoPane));
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
}
public static class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
private final ProductListActivity mParentActivity;
private final List<DummyContent.DummyItem> mValues;
private final boolean mTwoPane;
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
DummyContent.DummyItem item = (DummyContent.DummyItem) view.getTag();
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ProductDetailFragment.ARG_ITEM_ID, item.id);
ProductDetailFragment fragment = new ProductDetailFragment();
fragment.setArguments(arguments);
mParentActivity.getSupportFragmentManager().beginTransaction()
.replace(R.id.product_detail_container, fragment)
.commit();
// Custom color for the selected item
view.setBackgroundColor(Color.GREEN);
} else {
Context context = view.getContext();
Intent intent = new Intent(context, ProductDetailActivity.class);
intent.putExtra(ProductDetailFragment.ARG_ITEM_ID, item.id);
context.startActivity(intent);
}
}
};
SimpleItemRecyclerViewAdapter(ProductListActivity parent,
List<DummyContent.DummyItem> items,
boolean twoPane) {
mValues = items;
mParentActivity = parent;
mTwoPane = twoPane;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_list_content, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).content);
holder.itemView.setTag(mValues.get(position));
holder.itemView.setOnClickListener(mOnClickListener);
}
@Override
public int getItemCount() {
return mValues.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
final TextView mIdView;
final TextView mContentView;
ViewHolder(View view) {
super(view);
mIdView = (TextView) view.findViewById(R.id.id_text);
mContentView = (TextView) view.findViewById(R.id.content);
}
}
}
}
product_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:background="@drawable/border_all"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".activities.Inventory.ProductListActivity">
<!--
This layout is a two-pane layout for the Products
master/detail flow.
-->
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/product_list"
android:name="com.applab.nestlenido.activities.Inventory.ProductListFragment"
android:layout_width="@dimen/item_width"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
tools:context="com.applab.nestlenido.activities.Inventory.ProductListActivity"
tools:listitem="@layout/product_list_content" />
<FrameLayout
android:id="@+id/product_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
我的目标是更改所选项目的背景颜色。如果您触摸其他项目,则上一个项目应该没有背景,而新项目现在应该具有背景(正常行为)。