所以我制作了一个多个不同的CardView Layout并将其放入RecyclerView,每个cardview都有自己的类来处理视图。然后我在MainActivity中有一个浮动操作按钮,我想用它作为我的程序的启动器,但我无法访问其中一个cardview中的计时器。那么我如何在MainActivity的一个CardView中访问一个视图(Chrono)?
我已经尝试过使用来自mainactivity的findviewbyid,但它总是以null结果,因为它可能创建了recyclerview。
感谢您的帮助。
由于
CustomAdapter.java
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private int[] mCardTypes;
public static final int TIME = 0;
public static final int MAP = 1;
public static final int COST = 2;
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
public CustomAdapter(MainActivity main, int[] cardTypes) {
this.main = main;
mCardTypes = cardTypes;
}
public class ParkTimeViewHolder extends ViewHolder {
Chronometer mTotalTime;
Chronometer mRemainingTime;
public ParkTimeViewHolder(View itemView) {
super(itemView);
this.mTotalTime = (Chronometer) itemView.findViewById(R.id.chrono_total);
this.mRemainingTime = (Chronometer) itemView.findViewById(R.id.chrono_remaining);
// i can only run choro.start() here
}
}
public class MapViewHolder extends ViewHolder {
//MapView
public MapViewHolder(View itemView) {
super(itemView);
// TODO: 24-Dec-15 add map view
}
}
public class CostViewHolder extends ViewHolder {
TextView mCost;
public CostViewHolder(View itemView) {
super(itemView);
this.mCost = (TextView) itemView.findViewById(R.id.text_view_cost);
mCost.setText("20000");
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if (viewType == TIME) {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_time, parent, false);
return new ParkTimeViewHolder(v);
} else if (viewType == MAP) {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_map, parent, false);
return new MapViewHolder(v);
} else {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_cost, parent, false);
return new CostViewHolder(v);
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(holder.getItemViewType() == TIME) {
ParkTimeViewHolder myHolder = (ParkTimeViewHolder) holder;
} else
if(holder.getItemViewType() == MAP) {
MapViewHolder myHolder = (MapViewHolder) holder;
} else
if(holder.getItemViewType() == COST) {
CostViewHolder myHolder = (CostViewHolder) holder;
}
}
@Override
public int getItemCount() {
return mCardTypes.length;
}
@Override
public int getItemViewType(int position) {
return mCardTypes[position];
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RecyclerView mRecyclerView;
private CustomAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private Chronometer mTotalChrono;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.recycle_view_main_activity);
mLayoutManager = new LinearLayoutManager(MainActivity.this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new CustomAdapter(this, new int[]{CustomAdapter.TIME, CustomAdapter.COST});
mRecyclerView.setAdapter(mAdapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.fab) {
Snackbar.make(v, "Started", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
// i need to run chrono.start(); from here
}
}
@Override
protected void onStart() {
super.onStart();
}
答案 0 :(得分:0)
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private int[] mCardTypes;
public static final int TIME = 0;
public static final int MAP = 1;
public static final int COST = 2;
// create this variable
private static ParkTimeViewHolder parkTimeViewholder;
//call this function in mainactivity
public static void startChrono(){
if(parkTimeViewholder!=null){
// start chrono here using parkTimeViewholder variable
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
public CustomAdapter(MainActivity main, int[] cardTypes) {
this.main = main;
mCardTypes = cardTypes;
}
public class ParkTimeViewHolder extends ViewHolder {
Chronometer mTotalTime;
Chronometer mRemainingTime;
public ParkTimeViewHolder(View itemView) {
super(itemView);
this.mTotalTime = (Chronometer) itemView.findViewById(R.id.chrono_total);
this.mRemainingTime = (Chronometer) itemView.findViewById(R.id.chrono_remaining);
// i can only run choro.start() here
}
}
public class MapViewHolder extends ViewHolder {
//MapView
public MapViewHolder(View itemView) {
super(itemView);
// TODO: 24-Dec-15 add map view
}
}
public class CostViewHolder extends ViewHolder {
TextView mCost;
public CostViewHolder(View itemView) {
super(itemView);
this.mCost = (TextView) itemView.findViewById(R.id.text_view_cost);
mCost.setText("20000");
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if (viewType == TIME) {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_time, parent, false);
return new ParkTimeViewHolder(v);
} else if (viewType == MAP) {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_map, parent, false);
return new MapViewHolder(v);
} else {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_cost, parent, false);
return new CostViewHolder(v);
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(holder.getItemViewType() == TIME) {
ParkTimeViewHolder myHolder = (ParkTimeViewHolder) holder;
//initiate parkTimeViewholder variable
this.parkTimeViewholder = myHolder;
} else
if(holder.getItemViewType() == MAP) {
MapViewHolder myHolder = (MapViewHolder) holder;
} else
if(holder.getItemViewType() == COST) {
CostViewHolder myHolder = (CostViewHolder) holder;
}
}
@Override
public int getItemCount() {
return mCardTypes.length;
}
@Override
public int getItemViewType(int position) {
return mCardTypes[position];
}