当我向项目添加按钮或编辑文本时,为什么 ListFragment 中的 onListItemClick 函数没有触发?
Why doesn't the onListItemClick function in the ListFragment fire when I add a button or edittext to the item?
我用两个按钮和 edittext 渲染我的 ListFragment 的每个元素,但在这种情况下,onListItemClick 对我不起作用。一旦你在适配器中隐藏按钮和编辑文本,一切都会立即开始工作,我认为这是字段之间的某种冲突,但我不知道如何解决它。我将不胜感激。
我的适配器:
public class ProductAdapter extends ArrayAdapter<ProductsModel> {
private LayoutInflater inflater;
private int layout;
private ArrayList<ProductsModel> productsList;
private String nameFragment;
private Parsers parser;
public ProductAdapter(Context mContext, int resource, ArrayList<ProductsModel> products,
String nameFragment) {
super(mContext, resource, products);
this.productsList = products;
this.layout = resource;
this.inflater = LayoutInflater.from(mContext);
this.parser = new Parsers(mContext);
this.nameFragment = nameFragment;
}
public View getView(int position, View convertView, ViewGroup parent){
final ViewHolder viewHolder;
if(convertView==null){
convertView = inflater.inflate(this.layout, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
final ProductsModel products = productsList.get(position);
PricesModel price = parser.PriceParser((int) products.getId());
viewHolder.imageProduct.setImageResource(R.drawable.img);
viewHolder.imageProduct.setScaleType(ImageView.ScaleType.FIT_START);
viewHolder.nameProduct.setText(products.getName());
viewHolder.categoryProduct.setText("Group");
viewHolder.priceValueProduct.setText("Cost: "+price.getPriceValue());
viewHolder.countProduct.setText("0");
if(nameFragment.equals("ListProduct")){
viewHolder.priceValueProduct.setVisibility(View.GONE);
viewHolder.countProduct.setVisibility(View.GONE);
viewHolder.buttonPlus.setVisibility(View.GONE);
viewHolder.buttonMinus.setVisibility(View.GONE);
}
return convertView;
}
private class ViewHolder {
final ImageView imageProduct;
final TextView nameProduct, categoryProduct, priceValueProduct;
final EditText countProduct;
final Button buttonPlus, buttonMinus;
ViewHolder(View view){
imageProduct = view.findViewById(R.id.imageProduct);
nameProduct = view.findViewById(R.id.nameProduct);
categoryProduct = view.findViewById(R.id.categoryProduct);
priceValueProduct = view.findViewById(R.id.priceValueProduct);
countProduct = view.findViewById(R.id.countProduct);
buttonPlus = view.findViewById(R.id.buttonPlus);
buttonMinus = view.findViewById(R.id.buttonMinus);
}
}
}
我的片段:
public class ListProducts extends ListFragment {
private ProductAdapter mProductAdapter;
private Parsers parser;
private Context mContext;
private View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list_products, container, false);
mContext = getContext();
parser = new Parsers(mContext);
ArrayList<ProductsModel> productsList = parser.ProductsParser();
ArrayList<ProductsModel> improveProductsList = new ArrayList<>();
for(int i = 0; i < productsList.size(); i++){
improveProductsList.add(productsList.get(i));
}
EditText searchProduct = (EditText) view.findViewById(R.id.searchEditText);
searchProduct.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String nameProduct = s.toString();
if(count != 0) {
improveProductsList.clear();
for (int i = 0; i < productsList.size(); i++) {
if (productsList.get(i).getName().toLowerCase().indexOf(nameProduct.toLowerCase()) != -1) {
improveProductsList.add(productsList.get(i));
}
}
}else{
for(int i = 0; i < productsList.size(); i++){
improveProductsList.add(productsList.get(i));
}
}
}
@Override
public void afterTextChanged(Editable s) {}
});
try {
int idCompany = getArguments().getInt("idCompany",0);
int idStore = getArguments().getInt("idStore", 0);
mProductAdapter = new ProductAdapter(mContext, R.layout.simple_list_item_product, improveProductsList, "NewOrder");
}catch (NullPointerException e){
mProductAdapter = new ProductAdapter(mContext, R.layout.simple_list_item_product, improveProductsList, "ListProduct");
}
setListAdapter(mProductAdapter);
return view;
}
@Override
public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.d("TAG", ""+position);
}
}
XML simple_list_item_product:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageProduct"
android:layout_width="100sp"
android:layout_height="100sp"
android:scaleType="fitStart"/>
<LinearLayout
android:id="@+id/linearLayoutInfoAndButtonProduct"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="5sp">
<TextView
android:id="@+id/nameProduct"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="@color/black"
android:text="Name prod"/>
<TextView
android:id="@+id/categoryProduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@color/black"
android:text="Category prod"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf = "@+id/nameProduct" />
<TextView
android:id="@+id/priceValueProduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@color/black"
android:text="Cost prod"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf = "@+id/nameCompany" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"/>
<EditText
android:id="@+id/countProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/buttonPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
XML fragment_list_products:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_list_products"
tools:context="com.example.rbsoftsalesmobile.ui.productsscreen.ListProducts">
<TextView
android:gravity="center_horizontal"
android:id="@+id/game_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
android:text="List prod"/>
<EditText
android:id="@+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search"
app:layout_constraintTop_toBottomOf="@+id/game_title"
android:layout_margin="5sp"/>
<ListView
android:id="@android:id/list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchEditText" />
</androidx.constraintlayout.widget.ConstraintLayout>
我用 setFocusable(false) 解决了我的问题,对于每个可以拦截来自其他组件(如 Button、EditText、CheckBox 等)的输入的元素。
if(nameFragment.equals("ListProduct")){
viewHolder.priceValueProduct.setVisibility(View.GONE);
viewHolder.countProduct.setVisibility(View.GONE);
viewHolder.buttonPlus.setVisibility(View.GONE);
viewHolder.buttonMinus.setVisibility(View.GONE);
}
viewHolder.countProduct.setFocusable(false);
viewHolder.buttonPlus.setFocusable(false);
viewHolder.buttonMinus.setFocusable(false);
return convertView;
}
我用两个按钮和 edittext 渲染我的 ListFragment 的每个元素,但在这种情况下,onListItemClick 对我不起作用。一旦你在适配器中隐藏按钮和编辑文本,一切都会立即开始工作,我认为这是字段之间的某种冲突,但我不知道如何解决它。我将不胜感激。
我的适配器:
public class ProductAdapter extends ArrayAdapter<ProductsModel> {
private LayoutInflater inflater;
private int layout;
private ArrayList<ProductsModel> productsList;
private String nameFragment;
private Parsers parser;
public ProductAdapter(Context mContext, int resource, ArrayList<ProductsModel> products,
String nameFragment) {
super(mContext, resource, products);
this.productsList = products;
this.layout = resource;
this.inflater = LayoutInflater.from(mContext);
this.parser = new Parsers(mContext);
this.nameFragment = nameFragment;
}
public View getView(int position, View convertView, ViewGroup parent){
final ViewHolder viewHolder;
if(convertView==null){
convertView = inflater.inflate(this.layout, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
final ProductsModel products = productsList.get(position);
PricesModel price = parser.PriceParser((int) products.getId());
viewHolder.imageProduct.setImageResource(R.drawable.img);
viewHolder.imageProduct.setScaleType(ImageView.ScaleType.FIT_START);
viewHolder.nameProduct.setText(products.getName());
viewHolder.categoryProduct.setText("Group");
viewHolder.priceValueProduct.setText("Cost: "+price.getPriceValue());
viewHolder.countProduct.setText("0");
if(nameFragment.equals("ListProduct")){
viewHolder.priceValueProduct.setVisibility(View.GONE);
viewHolder.countProduct.setVisibility(View.GONE);
viewHolder.buttonPlus.setVisibility(View.GONE);
viewHolder.buttonMinus.setVisibility(View.GONE);
}
return convertView;
}
private class ViewHolder {
final ImageView imageProduct;
final TextView nameProduct, categoryProduct, priceValueProduct;
final EditText countProduct;
final Button buttonPlus, buttonMinus;
ViewHolder(View view){
imageProduct = view.findViewById(R.id.imageProduct);
nameProduct = view.findViewById(R.id.nameProduct);
categoryProduct = view.findViewById(R.id.categoryProduct);
priceValueProduct = view.findViewById(R.id.priceValueProduct);
countProduct = view.findViewById(R.id.countProduct);
buttonPlus = view.findViewById(R.id.buttonPlus);
buttonMinus = view.findViewById(R.id.buttonMinus);
}
}
}
我的片段:
public class ListProducts extends ListFragment {
private ProductAdapter mProductAdapter;
private Parsers parser;
private Context mContext;
private View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list_products, container, false);
mContext = getContext();
parser = new Parsers(mContext);
ArrayList<ProductsModel> productsList = parser.ProductsParser();
ArrayList<ProductsModel> improveProductsList = new ArrayList<>();
for(int i = 0; i < productsList.size(); i++){
improveProductsList.add(productsList.get(i));
}
EditText searchProduct = (EditText) view.findViewById(R.id.searchEditText);
searchProduct.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String nameProduct = s.toString();
if(count != 0) {
improveProductsList.clear();
for (int i = 0; i < productsList.size(); i++) {
if (productsList.get(i).getName().toLowerCase().indexOf(nameProduct.toLowerCase()) != -1) {
improveProductsList.add(productsList.get(i));
}
}
}else{
for(int i = 0; i < productsList.size(); i++){
improveProductsList.add(productsList.get(i));
}
}
}
@Override
public void afterTextChanged(Editable s) {}
});
try {
int idCompany = getArguments().getInt("idCompany",0);
int idStore = getArguments().getInt("idStore", 0);
mProductAdapter = new ProductAdapter(mContext, R.layout.simple_list_item_product, improveProductsList, "NewOrder");
}catch (NullPointerException e){
mProductAdapter = new ProductAdapter(mContext, R.layout.simple_list_item_product, improveProductsList, "ListProduct");
}
setListAdapter(mProductAdapter);
return view;
}
@Override
public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.d("TAG", ""+position);
}
}
XML simple_list_item_product:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageProduct"
android:layout_width="100sp"
android:layout_height="100sp"
android:scaleType="fitStart"/>
<LinearLayout
android:id="@+id/linearLayoutInfoAndButtonProduct"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="5sp">
<TextView
android:id="@+id/nameProduct"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="@color/black"
android:text="Name prod"/>
<TextView
android:id="@+id/categoryProduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@color/black"
android:text="Category prod"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf = "@+id/nameProduct" />
<TextView
android:id="@+id/priceValueProduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="@color/black"
android:text="Cost prod"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf = "@+id/nameCompany" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"/>
<EditText
android:id="@+id/countProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/buttonPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
XML fragment_list_products:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_list_products"
tools:context="com.example.rbsoftsalesmobile.ui.productsscreen.ListProducts">
<TextView
android:gravity="center_horizontal"
android:id="@+id/game_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
android:text="List prod"/>
<EditText
android:id="@+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search"
app:layout_constraintTop_toBottomOf="@+id/game_title"
android:layout_margin="5sp"/>
<ListView
android:id="@android:id/list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchEditText" />
</androidx.constraintlayout.widget.ConstraintLayout>
我用 setFocusable(false) 解决了我的问题,对于每个可以拦截来自其他组件(如 Button、EditText、CheckBox 等)的输入的元素。
if(nameFragment.equals("ListProduct")){
viewHolder.priceValueProduct.setVisibility(View.GONE);
viewHolder.countProduct.setVisibility(View.GONE);
viewHolder.buttonPlus.setVisibility(View.GONE);
viewHolder.buttonMinus.setVisibility(View.GONE);
}
viewHolder.countProduct.setFocusable(false);
viewHolder.buttonPlus.setFocusable(false);
viewHolder.buttonMinus.setFocusable(false);
return convertView;
}