我正在尝试在我的应用程序中创建一个RecyclerView,但我无法使其可滚动。这里有很多相同的问题讨论,但没有解决方案能帮助我。
我阅读了手册:
http://developer.android.com/training/material/lists-cards.html#RVExamples
我尝试在Fragment中实现它。问题是当我尝试滚动RecView时,LayoutManager会调用我的Adapter中的onBindViewHolder()和getItemCount()方法,但在视觉上没有任何变化。只在Lollipop设备上出现问题,在Android 4.X和6上运行良好。
有人可以告诉我我的错误在哪里吗?
非常感谢!
有趣的观察。当我滚动我的 RecyclerView 时,在程序级别上它确实在滚动!因为如果我点击一个项目,它会显示(在其他片段中)列表底部的项目!当我按“返回”时,它会将我带回到已滚动的 RecyclerView!我认为这意味着问题在布局管理器附近,具体来说是在 Item Decorator 或 Item Animator 中。
我仍在尝试解决我的问题,并查看 ListView 的源代码(包 android.widget.ListView.java)。我发现了失败的导入。例如:
public class SensorAdapter extends RecyclerView.Adapter<SensorAdapter.MyViewHolder>{
List<Sensor> data= Collections.emptyList();
public SensorAdapter(List<Sensor> data) {
L.l("SensorAdapter() Constructor", this);
this.data = data;
}
@Override
public SensorAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
L.l("SensorAdapter() onCreateViewHolder", this);
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sensor_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
L.l("SensorAdapter() onBindViewHolder", this);
Sensor curSensor = data.get(position);
holder.textView1.setText(curSensor.getCreated());
holder.textView2.setText(curSensor.getValue());
holder.textView3.setText("sensor id: "+curSensor.getSensor_id());
}
@Override
public int getItemCount() {
L.l("SensorAdapter() getItemCount", this);
return data.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView textView1;
TextView textView2;
TextView textView3;
public MyViewHolder(View itemView) {
super(itemView);
L.l("MyViewHolder() constructor", this);
textView1 = (TextView) itemView.findViewById(R.id.sensItemTV1);
textView2 = (TextView) itemView.findViewById(R.id.sensItemTV2);
textView3 = (TextView) itemView.findViewById(R.id.sensItemTV3);
}
}
Fragment:
public class SensorsFragment extends Fragment implements AdapterView.OnItemClickListener {
private final String types[] = {"SOLAR","HUMIDITY","TEMPERATURE","WINDSPEED","CHARGE","PRESSURE","HUMIDITY_15", "HUMIDITY_65"};
private Spinner spinner;
private RecyclerView recyclerView;
private Button from, to;
private List<Sensor> temp;
private static List<Sensor> sensorList;
private String result;
private TypeToken<List<Sensor>> tokenSensor;
private static Sensor curSensor;
private static String zone_id;
private SensorAdapter sensorAdapter;
private View layout;
public SensorsFragment() {}
public static SensorsFragment newInstance(String zoneId){
SensorsFragment.zone_id=zoneId;
return new SensorsFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
L.l("onCreateView()", this);
layout = inflater.inflate(R.layout.fragment_sensos, container, false);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
L.l("onActivityCreated", this);
initViews();
showRecyclerViews(getSensorsfromServer());
}
private void initViews(){
L.l("initViews()", this);
from = (Button) getActivity().findViewById(R.id.dateFrom);
to = (Button) getActivity().findViewById(R.id.dateTo);
}
private void prepareSpinner(){
L.l("prepareSpinner()", this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.snipper_item, types);
adapter.setDropDownViewResource(R.layout.snipper_item);
spinner = (Spinner) getActivity().findViewById(R.id.spinner2);
spinner.setAdapter(adapter);
spinner.setPrompt("Select type os sensors");
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String filtername = types[position].toLowerCase();
L.l("filtername = "+filtername);
temp = new ArrayList<>();
for(Sensor sensor: sensorList){
if(sensor.getType().equals(filtername)){
temp.add(sensor);
}
}
Log.d(LoginActivity.LOG, " temp.size() = " + temp.size());
//showRecyclerViews(temp);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public List<Sensor> getSensorsfromServer() {
L.l("getSensorsfromServer()", this);
tokenSensor = new TypeToken<List<Sensor>>() {};
try {
result = new MyDownTask("sensors/get",zone_id, getActivity()).execute().get();
sensorList = gson.fromJson(result, tokenSensor.getType());
if(sensorList==null) throw new Exception("sensorList==null");
L.l("sensorList.size() = "+sensorList.size(), this);
return sensorList;
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), "No data... :(", Toast.LENGTH_SHORT).show();
commitFragment(FieldsFragment.newInstance(),getFragmentManager());
return new ArrayList<>();
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
curSensor = sensorList.get(i);
Log.d(LoginActivity.LOG, "ActivityListView. onItemClick. curSensor = " + curSensor + " isSensorListVisible = ");
Intent intent = new Intent(getActivity(), DetailsActivity.class);
startActivity(intent);
}
private void showRecyclerViews(List<Sensor> sensors){
recyclerView = (RecyclerView) getActivity().findViewById(R.id.sensorsRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
sensorAdapter = new SensorAdapter(sensors);
recyclerView.setAdapter(sensorAdapter);
}
} fragment_sensos.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mobile_screen_1"
tools:context="ua.kiev.netmaster.agro.fragments.SensorsFragment">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:visibility="gone">
<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:background="@drawable/ripple"
android:contextClickable="false"
android:textColor="#ffffff"
android:visibility="visible" />
<Button
android:id="@+id/dateFrom"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:layout_weight="1"
android:background="@drawable/ripple"
android:text="от"
android:textColor="#FFF"
android:textSize="10dp"
android:visibility="visible" />
<Button
android:id="@+id/dateTo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:layout_weight="1"
android:background="@drawable/ripple"
android:text="до"
android:textColor="#FFF"
android:textSize="10dp"
android:visibility="visible" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/linearLayout">
<android.support.v7.widget.RecyclerView
android:id="@+id/sensorsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</FrameLayout>
有趣的观察。当我滚动我的 RecyclerView 时,在程序级别上它确实在滚动!因为如果我点击一个项目,它会显示(在其他片段中)列表底部的项目!当我按“返回”时,它会将我带回到已滚动的 RecyclerView!我认为这意味着问题在布局管理器附近,具体来说是在 Item Decorator 或 Item Animator 中。
我仍在尝试解决我的问题,并查看 ListView 的源代码(包 android.widget.ListView.java)。我发现了失败的导入。例如:
import com.google.android.collect.Lists;
import android.util.MathUtils;
import android.view.ViewRootImpl;
...以及其他*.java文件中的许多组件。 这是否会导致这些组件失去功能?我该如何解决这个问题?请帮帮我。