如何从ListView片段选项卡上单击的项打开和重新使用片段(取决于Item)

时间:2016-09-03 17:49:58

标签: android listview android-fragments

如何弹出新片段并重新使用它(取决于所选项目)单击ListView中的项目时。我已经尝试使用OnItemClickListener()而不是OnClickListene public void onItemClick(AdapterView parent,View view,int position,long id)

但它也没有用,任何帮助都会受到赞赏

please refer to the screenshot attached

由于

-- Fragment ListView Class--


public class WorkOutFragmentForearm extends Fragment {

    private String[] exercisesForearmNames; private String[] exerciseForearmType; 
    private String[] exerciseForeamNumber;

    private int[] image = { R.drawable.forearm,

    };

    private ArrayList<WorkOutListNameListExercises> mForearmsArray;

    private RecyclerView mForearmRecyclerView; private ForearmAdapter mAdapter;

    @Override public void onCreate(Bundle savedInstanceState) {

        Resources res = getResources();

        // Initialize array from info form XML strings exercisesForearmNames = 
        res.getStringArray(R.array.exercisesForearmNames); exerciseForearmType = 
        res.getStringArray(R.array.exerciseForearmType); exerciseForeamNumber = 
        res.getStringArray(R.array.exerciseForeamNumber);

        mForearmsArray = new ArrayList<>();

        for (int i = 0; i < exercisesForearmNames.length; i++) { 
        WorkOutListNameListExercises s = new WorkOutListNameListExercises();

            s.setName(exercisesForearmNames[i]); 
            s.setExerciseType(exerciseForearmType[i]); 
            s.setExerciseNumber(exerciseForeamNumber[i]); s.setImageId(image[i]); 
            mForearmsArray.add(s); }

        super.onCreate(savedInstanceState);

    }// end Main

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup 
    container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.workout_recycler_forearm, container, 
        false); mForearmRecyclerView = (RecyclerView) 
        view.findViewById(R.id.recycler_view);

        mForearmRecyclerView.addItemDecoration(new 
        SimpleDividerItemDecoration(getResources())); 
        mForearmRecyclerView.setLayoutManager(new 
        LinearLayoutManager(getActivity())); updateUI();

        // -------------------------------------------------------------------------
        ---------- // When Back Button is clicked 
        view.setFocusableInTouchMode(true); view.requestFocus(); 
        view.setOnKeyListener(new View.OnKeyListener() { @Override public boolean 
        onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_BACK) {

                    // Display Drawer when back button is clicked if drawer not // display 
                    not go back without FragmentTransaction Code 
                    MainActivity.mDrawerLayout.openDrawer(Gravity.LEFT);

                    return true; } else {

                    return false; } } });

        return view;

    }// end Main

    private void updateUI() { mAdapter = new ForearmAdapter(mForearmsArray); 
    mForearmRecyclerView.setAdapter(mAdapter); }

    // Inner Class private class ForearmHolder extends RecyclerView.ViewHolder {

        private WorkOutListNameListExercises mForearm; public ImageView mImageView; 
        public TextView mNameTextView; public TextView mTypeNumberTextView;

        public ForearmHolder( View itemView){ super(itemView);


            mImageView = (ImageView) itemView.findViewById(R.id.imageView); 
            mNameTextView = (TextView) 
            itemView.findViewById(R.id.textview_name); mTypeNumberTextView = 
            (TextView) itemView.findViewById(R.id.textview_type_number);




           itemView.setOnClickListener(new View.OnClickListener() {

               @Override public void onClick(View view) {

Bundle bundle = new Bundle();

                if (mForearm.getName().equals(exercisesForearmNames[0])) {

FragmentTransaction fragmentTransaction = 
getActivity().getSupportFragmentManager().beginTransaction(); Fragment 
fragmentItem1 = new FragmentItem1(); fragmentItem1.setArguments(bundle); 
fragmentTransaction.add(R.id.my_frame_list, fragmentItem1); 
fragmentTransaction.addToBackStack(null); fragmentTransaction.commit();


                    Toast.makeText(getActivity(),mForearm.getName() + "Item 1", 
                    Toast.LENGTH_SHORT).show(); }





                });//end  View.OnClickListener

        }// end Method

        public void bindData(WorkOutListNameListExercises s) { mForearm = s;

            mImageView.setImageResource(s.getImageId()); 
            mNameTextView.setText(s.getName()); 
            mTypeNumberTextView.setText(s.getExerciseType() + ":" + 
            s.getExerciseNumber());

        }

    }// end Inner Class

    // Inner Class private class ForearmAdapter extends 
    RecyclerView.Adapter<ForearmHolder>

    { private ArrayList<WorkOutListNameListExercises> mForearmsArray;

        public ForearmAdapter(ArrayList<WorkOutListNameListExercises> 
        WorkOutListNameListExercises) { mForearmsArray = 
        WorkOutListNameListExercises;

        }

        @Override public ForearmHolder onCreateViewHolder(ViewGroup parent, int 
        viewType) { LayoutInflater layoutInflater = 
        LayoutInflater.from(getActivity()); View view = 
        layoutInflater.inflate(R.layout.workout_items_list_info, parent, false); 
        return new ForearmHolder(view);

        }

        @Override public void onBindViewHolder(ForearmHolder holder, int position) {

            WorkOutListNameListExercises s = mForearmsArray.get(position); 
            holder.bindData(s);

        }

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

        } }// end Inner Class

}// end Class

WorkOutListNameListExercises.java

public class WorkOutListNameListExercises { private String ExreciseName; private 
String ExerciseType; private String ExerciseNumber; private int ExerciseImageId;

    public String getName() { return ExreciseName; }

    public void setName(String ExreciseName) { this.ExreciseName = ExreciseName; }

    public String getExerciseType() { return ExerciseType; }

    public void setExerciseType(String ExerciseType) { this.ExerciseType = 
    ExerciseType; }

    public String getExerciseNumber() { return ExerciseNumber; }

    public void setExerciseNumber(String ExerciseNumber) { this.ExerciseNumber = 
    ExerciseNumber; }

    public int getImageId() { return ExerciseImageId; }

    public void setImageId(int ExerciseImageId) { this.ExerciseImageId = 
    ExerciseImageId; } }

1 个答案:

答案 0 :(得分:0)

好!所以你使用的是recyclerview,在itemview上你还添加了一个onClickListener。你也在那里创建捆绑。那么你已经99%了!

您尚未在捆绑中添加任何内容。要在其中添加一些值,请执行

bundle.putString("key1","value1");

您可以使用putStringArray()putStringArrayList()

传递数组和ArrayList

我们无需检查用户点击的第一个,第二个或第三个项目。在已经为我们提供项目位置的bindData()中执行此操作,使用此位置我们将适当的值传递给片段。所以我们的代码看起来像这样。

public void bindData(WorkOutListNameListExercises s) {

itemView.setOnClickListener(new View.OnClickListener() {

   @Override
     public void onClick(View view) {
       Bundle bundle = new Bundle();
       bundle.putString("forearmName",s.getName());
       bundle.putString("forearmType",s.getType());
       bundle.putString("forearmNumber",s.getNumber());

       FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
       Fragment fragmentItem1 = new FragmentItem1(); 
       fragmentItem1.setArguments(bundle);
       fragmentTransaction.add(R.id.my_frame_list, fragmentItem1);
       fragmentTransaction.addToBackStack(null);
       fragmentTransaction.commit();

       //Your Toast message!
    }
  }
}

现在FragmentItem1重复使用它。在onCreate中执行此操作

private String exercisesForearmName, exerciseForearmType, exerciseForeamNumber;
@Override
public void onCreate(Bundle savedInstanceState) {
Bundle bundle=this.getArguments();
exercisesForearmNames=bundle.getString("forearmName");
exerciseForearmType=bundle.getString("forearmType");
exerciseForearmNumber=bundle.getString("forearmNumber");
}

现在只需将值分配给您在onCreateView()

中打算查看的视图

完成。希望这会有所帮助。