我正在开发一个Android应用程序的屏幕,我需要在recyclelerview的每一行添加一个click事件,并且它在部分工作。
问题在于,一旦加载了intent,可点击区域就会限制其内容的宽度。
只有在滚动发生后,recyclelerview行在视口中完全隐藏时,可点击区域才能正常工作(宽度为100%)。
贝娄有一个GIF来说明我的问题。请注意,在滚动之前,可点击区域将缩小为行内容,滚动后可点击区域将填充该行。
<img src="https://i.imgur.com/wGvqa5Z.gif" width="150" style="width:150px; border:1px solid #000;" />
active_catalog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
tools:context="viniciusdesouza.com.br.barbearia.CatalogActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_catalog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_catalog" />
</android.support.design.widget.CoordinatorLayout>
content_catalog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="viniciusdesouza.com.br.barbearia.CatalogActivity"
tools:showIn="@layout/activity_catalog">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_catalog_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
catalog_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="72sp"
android:gravity="center_vertical"
android:orientation="vertical"
android:background="?android:attr/selectableItemBackground">
<TextView
android:id="@+id/txt_catalog_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="TextView"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_catalog_item_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="TextView" />
</LinearLayout>
CatalogAdapter.java
public class CatalogAdapter extends RecyclerView.Adapter<CatalogAdapter.ViewHolderCatalog> {
private List<CatalogItem> catalog;
private OnRecyclerViewItemClickListener listener;
public CatalogAdapter (List<CatalogItem> catalog) {
this.catalog = catalog;
}
@Override
public CatalogAdapter.ViewHolderCatalog onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.catalog_item, parent, false);
ViewHolderCatalog viewHolderCatalog = new ViewHolderCatalog(view);
return viewHolderCatalog;
}
@Override
public void onBindViewHolder(CatalogAdapter.ViewHolderCatalog holder, final int position) {
if ( (catalog != null) && (catalog.size() > 0)) {
CatalogItem item = catalog.get(position);
holder.txtItemName.setText(item.name);
holder.txtItemPrice.setText(String.valueOf(item.price));
holder.itemView.setOnClickListener(new View.OnClickListener () {
@Override
public void onClick(View v){
listener.onRecyclerViewItemClicked(position, -1);
}
});
}
}
@Override
public int getItemCount() {
return catalog.size();
}
public static class ViewHolderCatalog extends RecyclerView.ViewHolder {
public TextView txtItemName;
public TextView txtItemPrice;
public ViewHolderCatalog(View itemView) {
super(itemView);
txtItemName = (TextView) itemView.findViewById(R.id.txt_catalog_item_name);
txtItemPrice = (TextView) itemView.findViewById(R.id.txt_catalog_item_price);
}
}
/**
* Custom created method for Setting the item click listener for the items and items with in items
* @param listener OnRecyclerViewItemClickListener
*/
public void setOnItemClickListener(OnRecyclerViewItemClickListener listener)
{
this.listener = listener;
}
}
CatalogActivity.java
public class CatalogActivity extends AppCompatActivity implements OnRecyclerViewItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_catalog);
setSupportActionBar(toolbar);
// back button
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
recyclerViewCatalog = (RecyclerView) findViewById(R.id.rv_catalog_list);
recyclerViewCatalog.setHasFixedSize(true);
dbConnect();
catalogRepository = new CatalogRepository(databaseHandler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
catalog = catalogRepository.all();
recyclerViewCatalog.setLayoutManager(linearLayoutManager);
catalogAdapter = new CatalogAdapter(catalog);
mDividerItemDecoration = new DividerItemDecoration(
recyclerViewCatalog.getContext(),
linearLayoutManager.getOrientation()
);
recyclerViewCatalog.addItemDecoration(mDividerItemDecoration);
recyclerViewCatalog.setAdapter(catalogAdapter);
catalogAdapter.setOnItemClickListener(this);
}
@Override
public void onRecyclerViewItemClicked(int position, int id)
{
// do something with item clicked
}
}
任何帮助将不胜感激
答案 0 :(得分:1)
该问题与content_catalog.xml文件中的ConstraintLayout有关。
类似的问题正在发生here,所以要修复它,尝试简单地用其他东西替换它,比如LinearLayout或RelativeLayout,它应该解决视图的问题,直到滚动重新渲染之后才扩展。
答案 1 :(得分:0)
只需更改您的ViewHolderCatalog类,如下所示
public static class ViewHolderCatalog extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtItemName;
public TextView txtItemPrice;
public ViewHolderCatalog(View itemView) {
super(itemView);
txtItemName = (TextView) itemView.findViewById(R.id.txt_catalog_item_name);
txtItemPrice = (TextView) itemView.findViewById(R.id.txt_catalog_item_price);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), “You clicked your your recycler view item”, Toast.LENGTH_SHORT).show();
}
}