0
私はリサイクルビューでカードにonclickリスナーを追加しようとしていますが、カードをクリックすると新しいページ/アクティビティに移動することが目的です。残念なことに私はJavaにはまだ新しいですが、それをよく理解していません。私は多くの似たような質問を見てきましたが、それを使う方法を理解できません。 これはView Holderも含むRecycleViewアダプタです。RecycleViewでカードにonclickリスナーを実装する方法
public class JobListRecyclerAdapter extends RecyclerView.Adapter<JobListRecyclerAdapter.jobViewHolder> {
private ArrayList<JobClass> jobClassList;
public JobListRecyclerAdapter(ArrayList<JobClass> rList) {
this.jobClassList = rList;
}
@Override
public int getItemCount() {
if(jobClassList == null){return 0;}
return jobClassList.size();
}
@Override
public jobViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.job_list_card, viewGroup, false);
return new jobViewHolder(itemView);
}
@Override
public void onBindViewHolder(jobViewHolder jobViewHolder, int position) {
JobClass ci = jobClassList.get(position);
jobViewHolder.jName.setText("Name: "+ci.getmJobNum());
jobViewHolder.jDate.setText("Quantity: "+ci.getmJobDate());
}
public static class jobViewHolder extends RecyclerView.ViewHolder {
protected TextView jName;
protected TextView jDate;
public jobViewHolder(View j) {
super(j);
jName = (TextView) j.findViewById(R.id.job_name);
jDate = (TextView) j.findViewById(R.id.job_due_date);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mClickListener.onItemClick(v, getAdapterPosition());
}
});
}
private jobViewHolder.ClickListener mClickListener;
//Intreface to send callbacks
public interface ClickListener{
public void onItemClick(View view, int position);
}
public void setOnClickListener(jobViewHolder.ClickListener clicklistener){
mClickListener = clicklistener;
}
}
}
これは「ジョブリスト」という名前のアクティビティです。これはフラグメントなので、私は相対的な例を見つけることができません。これは私の主な問題です。私は、ビューを実装する方法と、カードがクリックされたときにアクティビティを実行する方法についての手がかりがありません。
public class JobList extends Fragment {
public JobList() {
}
public static JobList newInstance() {
JobList fragment = new JobList();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_job_list, container, false);
final RecyclerView recyclerView = (RecyclerView)rootView.findViewById(R.id.job_list_recycler_view);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(llm);
final ArrayList<JobClass> joblistclass = new ArrayList<>();
/*
// Create a new Adapter
final ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1);
*/
// Get a reference to the todoItems child items it the database
//String userPath = ((GlobalData) getActivity().getApplication()).getUserPath() +"JOBS";
//final DatabaseReference myRef = database.getReference(userPath);
// final DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("USERS/04950F4AE53F80/JOBS");
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference jobsRef = rootRef
.child("USERS")
.child("04950F4AE53F80")
.child("JOBS");
// Assign a listener to detect changes to the child items
// of the database reference.
// myRef.addValueEventListener(new ValueEventListener() {
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String jobDate = ds.child("JOBOVERVIEW").child("jobDate").getValue(String.class);
JobClass jblst = ds.child("JOBOVERVIEW").getValue(JobClass.class);
joblistclass.add(jblst);
Log.d("TAG", jobDate);
}
final JobListRecyclerAdapter adapterb = new JobListRecyclerAdapter(joblistclass);
recyclerView.setAdapter(adapterb);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
jobsRef.addListenerForSingleValueEvent(eventListener);
///to send to next page
Button nextPage = (Button) rootView.findViewById(R.id.addNewJob);
// Capture button clicks
nextPage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Start NewActivity.class
Intent myIntent = new Intent(getContext(), JobForm.class);
startActivity(myIntent);
}
});
// go to job page when clicked
recyclerView.setOnItemClickListener(new View.OnClickListener{
@Override
public void onClick(AdapterView<?> adapter, View view, int position, long id) {
String name = (String) listView.getItemAtPosition(position);
((GlobalData) getActivity().getApplication()).setJobId(name);
// use -((GlobalData) getActivity().getApplication()).getJobId() to refer to the job id
Intent goToJob = new Intent(getContext(), Main2Activity.class);
startActivity(goToJob);
}
});
return rootView;
}
}
Main2Activityは私が必要とするページです。
public class Main2Activity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment2 = null;
switch (item.getItemId()) {
case R.id.navigation2_home://maybe edit so that it goes back to first activity
selectedFragment2 = HomePage.newInstance();
break;
case R.id.navigation_overview:
selectedFragment2 = Overview.newInstance();
break;
case R.id.navigation_safety:
selectedFragment2 = Safety.newInstance();
break;
case R.id.navigation_quality:
selectedFragment2 = Quality.newInstance();
break;
case R.id.navigation_instruction:
selectedFragment2 = Instructions.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content2, selectedFragment2);
transaction.commit();
return true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation2);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
上記のコードのすべてが問題なく、私は助けが必要です。Pleeeeeaaaase。あなたのJobListRecyclerAdapter
クラスで
アダプタ –
のsetOnClickListener()をコールしましたか?アダプタおよびsetClickListenerメソッドのコールバックリスナインタフェースも定義してください –
コールバックをフラグメントで取得できます –