我的回收视图中充满了开关,计时器和文本视图。我正在使用来自arraylist的数据填充此循环视图,该arraylist传递了一个用于开关的布尔值,一个用于textview的字符串,以及一个很长的时间。这是我的问题,我需要在哪里放置交换机的监听器?如果我把监听器放在我的适配器类中,我就不能引用存储值的arraylist,如果我把监听器放在我的主时间守护.java类中,我就不能引用我的开关。我是android dev的新手,所以我可能只是遗漏了一些明显的东西。我将提供我迄今为止所写的所有代码。
这是我的主要timeKeeping.java类
public class timeKeeping extends AppCompatActivity {
private List<timeTrackCell> timesList = new ArrayList<>();
//TODO replace empName with data from server
public String empName = "Zach";
private RecyclerView rv;
public timeTrackCellAdapter tAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Button that shows who is logged in
setContentView(R.layout.activity_time_keeping2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setImageResource(R.drawable.ic_temp_profile_image);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String greetingString = "Welcome back, " + empName + "!";
Snackbar.make(view, greetingString, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
rv = (RecyclerView) findViewById(R.id.timeListTest);
tAdapter = new timeTrackCellAdapter(timesList);
RecyclerView.LayoutManager tLayoutManager = new LinearLayoutManager(getApplicationContext());
rv.setLayoutManager(tLayoutManager);
rv.setItemAnimator(new DefaultItemAnimator());
rv.setAdapter(tAdapter);
prepareData();
}
这是与之对应的xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="I took this out because it contained my name">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:datePickerMode="spinner"
android:id="@+id/dateSelect"
android:calendarViewShown="false"
android:layout_marginTop="-100dp"
android:layout_marginBottom="-110dp"
android:translationY="-35dp"
></DatePicker>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_time_keeping2" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
<android.support.v7.widget.RecyclerView
android:id="@+id/timeListTest"
android:layout_gravity="start"
android:layout_below="@id/toolbar"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:clipToPadding="false"
android:paddingTop="55dp"
android:paddingBottom="70dp"
android:divider="@android:color/transparent"
android:focusable="true"
android:scrollbars="vertical"
></android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
这是我的timeKeepingCell.java类,它将我的主类和我的适配器拼接在一起
public class timeTrackCell {
private boolean switchPosition;
private long chronometerTime;
private String jobString;
public timeTrackCell(boolean switchPosition, long chronometerTime, String jobString){
this.switchPosition = switchPosition;
this.chronometerTime = chronometerTime;
this.jobString = jobString;
}
//sets
public void setSwitchPosition(boolean switchPosition){
this.switchPosition = switchPosition;
}
public void setChronometerTime(long chronometerTime){
this.chronometerTime = chronometerTime;
}
public void setJobString(String jobString){
this.jobString = jobString;
}
//gets
public boolean getSwitchPosition(){
return switchPosition;
}
public long getChronometerTime(){
return chronometerTime;
}
public String getJobString(){
return jobString;
}
}
这是我的timeTrackCellAdapter.java类,它是我的适配器
public class timeTrackCellAdapter extends RecyclerView.Adapter<timeTrackCellAdapter.MyViewHolder> {
private List<timeTrackCell> jobDataList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView jobDesc;
public Switch jobSwitch;
public Chronometer jobTime;
public MyViewHolder(View view) {
super(view);
jobDesc = (TextView) view.findViewById(R.id.secondaryRowText);
jobSwitch = (Switch) view.findViewById(R.id.timeSwitch);
jobTime = (Chronometer) view.findViewById(R.id.timeTracker);
}
}
public timeTrackCellAdapter(List<timeTrackCell> jobDataList) {
this.jobDataList = jobDataList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cell_layout, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
timeTrackCell tView = jobDataList.get(position);
holder.jobDesc.setText(tView.getJobString());
holder.jobSwitch.setChecked(tView.getSwitchPosition());
long elapsedRealtimeOffset = SystemClock.elapsedRealtime() - tView.getChronometerTime();
holder.jobTime.setBase(elapsedRealtimeOffset);
}
@Override
public int getItemCount() {
return jobDataList.size();
}
}
这是我构建的自定义单元格的xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="140px"
>
<!--140px Seems to be the right height for 7 cells per page-->
<!-- Block for custom listview items -->
<Chronometer
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/timeTracker"
android:layout_gravity="left"
android:textSize="25sp"
android:paddingLeft="10px"
android:layout_centerVertical="true">
</Chronometer>
<TextView
android:id="@+id/secondaryRowText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/timeTracker"
android:textSize="15sp"
android:paddingLeft="10px"
android:paddingTop="30px"
>
</TextView>
<Switch
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/timeSwitch"
android:gravity="right"
android:layout_centerVertical="true"
>
</Switch>
</RelativeLayout>
答案 0 :(得分:0)
对于Switch
,您需要实施setOnCheckedChangeListener而不是onClickListener
如果你想为RecyclerView中的每个开关做一些不同的事情,或者onBindViewHolder
里面的代码是独立的,那么下面的代码可以进入ViewHolder
jobSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something, the isChecked param will be
// true if the switch is in the On position
}
});