我正在与recyclerview
合作,我无法开始新的活动
每次点击。如何开始新活动recyclerview?
以下是我的代码:
主要活动:
public class MainActivity extends AppCompatActivity {
private List<Data> dataList = new ArrayList<>();
private RecyclerView recycler_view;
private Custom_Adapter custom_adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycler_view = (RecyclerView) findViewById(R.id.recycler_view);
custom_adapter = new Custom_Adapter(dataList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recycler_view.setLayoutManager(mLayoutManager);
recycler_view.setItemAnimator(new DefaultItemAnimator());
recycler_view.setAdapter(custom_adapter);
prepareMovieData();
}
private void prepareMovieData() {
Data data = new Data("Preamble");
dataList.add(data);
data = new Data("Parts");
dataList.add(data);
data = new Data("Schedules");
dataList.add(data);
data = new Data("Amendments");
dataList.add(data);
data = new Data("Quiz");
dataList.add(data);
custom_adapter.notifyDataSetChanged();
}
}
适配器:
public class Custom_Adapter extends RecyclerView.Adapter<Custom_Adapter.MyViewHolder> {
private List<Data> dataList;
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public View view;
public TextView title;
public MyViewHolder(final View itemView) {
super(itemView);
itemView.setOnClickListener(this);
title=(TextView)itemView.findViewById(R.id.tv_title);
}
@Override
public void onClick(View view) {
}
}
public Custom_Adapter(List<Data> dataList) {
this.dataList = dataList;
}
@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, final int position) {
final View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.data_list_row,parent,false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
Data data = dataList.get(position);
holder.title.setText(data.getTitle());
}
@Override
public int getItemCount() {
return dataList.size();
}
}
Data.java:
public class Data {
String title;
public Data(){}
public Data(String title){
this.title=title;
}
public String getTitle() {
return title;
}
public void setTitle(String name) {
this.title = name;
}
}
我想在每次点击时开始新活动。我应该怎么做才能开始新的?我可以使用开关盒吗?
答案 0 :(得分:1)
下面是一些代码,其中有一个示例,说明如何使用此开关。但是,要使用它Vertabim你需要一个适当的回调模式 - 即。将onClickListener创建为匿名类,并将其传递给适配器构造函数。然后让适配器保留一个引用并将其传递给VH onBind
//in Adapter, Viewholder
private View.OnClickListener mCallback;
public Custom_Adapter(List<Data> dataList, View.OnClickListener callback) {
this.dataList = dataList;
mCallback = callback;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.itemView.setOnClickListener(mCallback);
holder.itemView.setTag(position);
...
}
//in Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
...
custom_adapter = new Custom_Adapter(dataList, new OnClickListener() {
@Override
public void onClick(View view) {
Class<? extends Activity> target;
switch( (int) view.getTag() ) {
case 0:
target = PreambleActivity.class;
break;
...
}
if (target != null) {
Intent intent = new Intent(MainActivity.this, target);
startActivity(intent);
}
}
});
}
答案 1 :(得分:0)
您应该在Viewholder的onClick方法中启动新活动,如下所示
select count(*)
from files
where folder_id in (select id from folders where folder_path like 'A/B%');