gpt4 book ai didi

android - 如何从 Fragment 设置适配器中 RelativeLayout 的可见性

转载 作者:行者123 更新时间:2023-11-29 16:53:09 26 4
gpt4 key购买 nike

我想使用我的适配器从我的 fragment 中设置我的 relativelayout 的可见性,请参见下图

enter image description here

这是我的 fragment

我将 relativeLayout 声明为 public,这样我就可以在我的适配器上访问它以设置它的可见性,但我总是得到一个 null 错误单击复选框时如何将相对布局可见性设置为可见。请帮我。非常感谢。

public class FoodListFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

public RelativeLayout relativeLayout;

private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private Food_RecyclerAdapter adapter;
private List<Food> foods;
private ApiInterface apiInterface;
ProgressBar progressBar;


// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public FoodListFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FoodListFragment.
*/
// TODO: Rename and change types and number of parameters
public static FoodListFragment newInstance(String param1, String param2) {
FoodListFragment fragment = new FoodListFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_food_list, container, false);
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

recyclerView = (RecyclerView)view.findViewById(R.id.recycleViewFood);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);

relativeLayout = (RelativeLayout)view.findViewById(R.id.cart_add);
relativeLayout.setVisibility(View.GONE);

apiInterface = ApiClient.getClient().create(ApiInterface.class);

progressBar = (ProgressBar)view.findViewById(R.id.progress_bar);
progressBar.setVisibility(View.VISIBLE);

Bundle bundle = this.getArguments();
String category_id = bundle.getString("menu_id");

Call<List<Food>> call = apiInterface.getFoodList(category_id);
call.enqueue(new Callback<List<Food>>() {
@Override
public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
foods = response.body();
adapter = new Food_RecyclerAdapter(getContext(),foods);
recyclerView.setAdapter(adapter);
progressBar.setVisibility(View.GONE);
}

@Override
public void onFailure(Call<List<Food>> call, Throwable t) {
progressBar.setVisibility(View.GONE);
Toast.makeText(getContext(), "Please check your network connection", Toast.LENGTH_SHORT).show();

}
});


EditText searhText = (EditText)view.findViewById(R.id.search_food);

searhText.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) {
adapter.getfilter().filter(s.toString());
}

@Override
public void afterTextChanged(Editable s) {

}
});

return view;

}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {

}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}

这是我的适配器

public class Food_RecyclerAdapter extends RecyclerView.Adapter<Food_RecyclerAdapter.myViewHolder>{
Context context;
public List<Food> foods;
public List<Food> mOriginalValues;
public List<Food> mDisplayedValues;
ApiClient apiClient;
FoodListFragment foodListFragment;

public Food_RecyclerAdapter(Context context,List<Food> foods){
this.context = context;
this.foods = foods;
this.mOriginalValues = foods;
this.mDisplayedValues = foods;

}


@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_list_row,parent,false);
return new myViewHolder(view);
}

@Override
public void onBindViewHolder(final myViewHolder holder, int position) {
apiClient = new ApiClient();

Picasso.with(context).load(apiClient.BASE_URL + foods.get(position).getImage()).into(holder.food_image);



holder.food_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(holder.food_check.isChecked()){

//HERE I WANT TO SHOW THE RELATIVE LAYOUT WHEN I CLICK MY CHECKBOX
foodlistFragment = new FoodListFragment();
foodListFragment.relativeLayout.setVisibility(View.VISIBLE);

}else{
holder.food_qty.setText(""+0);
}
}
});


}

@Override
public long getItemId(int position) {
return position;
}

@Override
public int getItemViewType(int position) {
return position;
}

@Override
public int getItemCount() {
return foods.size();
}

public class myViewHolder extends RecyclerView.ViewHolder{
TextView food_price,food_availability,food_qty;
ImageView food_image,remove_image,add_image;
CheckBox food_check;
public myViewHolder(View itemView) {
super(itemView);
food_check = (CheckBox) itemView.findViewById(R.id.radioFood_name);
food_image = (ImageView)itemView.findViewById(R.id.food_image);
food_availability = (TextView) itemView.findViewById(R.id.food_availability);
food_price = (TextView) itemView.findViewById(R.id.food_price);
food_qty = (TextView) itemView.findViewById(R.id.food_qty);
add_image = (ImageView)itemView.findViewById(R.id.add_image);
remove_image = (ImageView)itemView.findViewById(R.id.remove_image);

}
}

}

这是 XML

<RelativeLayout
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="com.example.jampol.blogs.FoodListFragment">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/search_food"
android:drawableStart="@drawable/ic_search"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:drawablePadding="10dp"
android:hint="Search.."
android:gravity="center_vertical"
android:textSize="18dp"
android:background="@color/colorPrimary"
android:textColorHint="@color/white"
android:textColor="@color/white"
/>

<ProgressBar
android:id="@+id/progress_bar"
style="?android:progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recycleViewFood"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_below="@+id/search_food"
android:layout_alignParentStart="true"
android:layout_above="@+id/cart_add">

</android.support.v7.widget.RecyclerView>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cart_add"
android:layout_alignParentBottom="true"
android:padding="5dp"
android:layout_alignParentStart="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
android:textSize="16dp"
android:paddingRight="10dp"
android:layout_centerVertical="true"
android:textStyle="bold"
android:id="@+id/total"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/total"

android:text="100000"
android:textSize="16dp"
android:layout_centerVertical="true"
android:textStyle="bold"
/>

<Button

android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@color/red"
android:textColor="@color/white"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:text="Add to cart" />

</RelativeLayout>

最佳答案

希望对您有所帮助。您不需要在适配器中传递 fragment 的对象,也不需要在 fragment 中将 Relativelayout 定义为公共(public)。

按照这个步骤可能会很长,但你可以达到你想要的结果:

  1. 只需将您的 fragment Activity 作为上下文传递到您的适配器中。像这样:

    adapter = new Food_RecyclerAdapter(getActivity(),foods);

  2. 然后在您想要显示或隐藏 View 的适配器中做一些事情像这样:将您的上下文转换到 fragment 的 Activity 中。让你的 fragment 在里面你的主要 Activity 。所以这将是:

    MainActivity mActivity = ((MainActivity)context);

  3. 在您的 MainActivity 和 getCurrentFragment 中创建一个公共(public)方法像这样的 Activity :

    public void showHideViews(){
    fragment currentFragment = getActiveFragment();
    如果(当前 fragment != null){
    //从这里访问 fragment 的公共(public)方法
    ((FoodListFragment)currentFragment).showHideViews();
    }
    }

  4. 在您的 FoodListFragment 中创建 showHideViews() 方法:

    public void showHideViews(){
    yourRelativeLayout.setVisibility(View.GONE);
    }

第一次看起来很长,但这样会很方便。希望对你有帮助。

关于android - 如何从 Fragment 设置适配器中 RelativeLayout 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45768451/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com