【问题标题】:how to move from a fragment to a new activity when i clicked an item of recyclerview?当我点击一个recyclerview项目时,如何从一个片段移动到一个新的活动?
【发布时间】:2021-03-02 06:42:40
【问题描述】:

我在片段中动态使用recyclerview,现在我想在点击具有相同对象/项目的项目时打开新活动。

AllCoursesFeagment.java

package com.example.learningapp.ui.NavigationDrawer.AllCourses;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.learningapp.Adapters.AllCoursesMyAdapter;
import com.example.learningapp.Interface.APIinterface;
import com.example.learningapp.Models.CourseListModel;
import com.example.learningapp.R;
import com.example.learningapp.Responses.CourseListResponse;
import com.example.learningapp.RetrofitAPIs.RetrofitAPI;
import com.example.learningapp.utility.ConstantData;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;



public class AllCoursesFragment extends Fragment {
    private RecyclerView recyclerView;
    private List<CourseListModel> courseListModelList;
    public AllCoursesFragment() {
        // Required empty public constructor
    }
    // TODO: Rename and change types and number of parameters
    public static AllCoursesFragment newInstance() {
        return new AllCoursesFragment();
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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_all_courses,null);
        recyclerView = view.findViewById(R.id.allCourseList_RV);
        APIinterface apIinterface = RetrofitAPI.getClient().create(APIinterface.class);
        Call<CourseListResponse> call = apIinterface.getAllCourses(ConstantData.AppKey);
        call.enqueue(new Callback<CourseListResponse>() {
            @Override
            public void onResponse(Call<CourseListResponse> call, Response<CourseListResponse> response){
            if (response.isSuccessful()){
                courseListModelList = response.body().getCourseList();
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                recyclerView.setAdapter(new AllCoursesMyAdapter(getActivity(),courseListModelList));
            }
        }
            @Override
            public void onFailure(Call<CourseListResponse> call, Throwable t) {
                Toast.makeText(getActivity(),"Something went wrong",Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }
}

AllCoursesMyAdapter.java

这是我的适配器类。

package com.example.learningapp.Adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.learningapp.Activities.CheckoutActivity;
import com.example.learningapp.Models.CourseListModel;
import com.example.learningapp.R;

import java.util.List;

public class AllCoursesMyAdapter extends RecyclerView.Adapter<AllCoursesMyAdapter.RecyclerVH> {
    Context context;
    List<CourseListModel> courseListModelList;

    public AllCoursesMyAdapter(Context context, List<CourseListModel> courseListModelList) {
        this.context = context;
        this.courseListModelList = courseListModelList;
    }

    @NonNull
    @Override
    public RecyclerVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.single_row_design_allcourses,parent,false);
        return new RecyclerVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerVH holder, int position) {
        final CourseListModel courseListModel = courseListModelList.get(position);
        holder.courseTitle.setText(courseListModel.getCourseName());
        holder.courseDesc.setText(courseListModel.getCourseDescription());

        holder.buyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moveToNewActivity();
                //Toast.makeText(context,"Item Clicked"+position,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, CheckoutActivity.class);
                intent.putExtra("coursetitle",courseListModel.getCourseName());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });


    }
    private void moveToNewActivity () {

        Activity activity = new Activity();
        Intent i = new Intent(activity, CheckoutActivity.class);
        context.startActivity(i);
        ((Activity) context).overridePendingTransition(0, 0);

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

    public class RecyclerVH extends RecyclerView.ViewHolder {
        TextView courseTitle,courseDesc;
        Button buyBtn;
        public RecyclerVH(@NonNull View itemView) {
            super(itemView);
            courseTitle = itemView.findViewById(R.id.course_title);
            courseDesc = itemView.findViewById(R.id.course_desc);
            buyBtn = itemView.findViewById(R.id.btn_buycourse);
        }
    }
}

点击项目时我崩溃了

holder.buyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moveToNewActivity();
                //Toast.makeText(context,"Item Clicked"+position,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, CheckoutActivity.class);
                intent.putExtra("coursetitle",courseListModel.getCourseName());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });

在这里,我正在使用存储的对象列表,现在我想在单击具有给定位置的项目时移动到新活动,但我不能这样做。

【问题讨论】:

  • 你为什么打电话给moveToNewActivity,然后是完整的意图
  • 这不是你转移到新活动的方式:Activity activity = new Activity();
  • 那我怎么移动?请完成 moveToNewActivity(){ }

标签: java android android-recyclerview fragment retrofit


【解决方案1】:
private void moveToNewActivity () {

    Activity activity = new Activity(); //this is wrong, delete this
    ...

}

您不能也不应该创建这样的活动实例,这是操作系统必须为您安排和处理的事情。


您似乎已经在某处定义了上下文,这意味着:

private void moveToNewActivity () {
    Intent i = new Intent(context, CheckoutActivity.class);
    context.startActivity(i);
    ...

}

也就是说:

 @Override
 public void onClick(View v) {
    moveToNewActivity();
 } 

【讨论】:

    猜你喜欢
    • 2018-05-17
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 2021-10-10
    相关资源
    最近更新 更多