I'm trying to make a RecyclerView in my App, but I can't make it scrollable. There are a lot of same problems discused here, but no solution helps me. I read the manual: http://developer.android.com/training/material/lists-cards.html#RVExamples I try to do it in Fragment. The issue is when I try to scroll RecView, LayoutManager calls onBindViewHolder() and getItemCount() methods in my Adapter, but visually nothing happens. Only on Lollipop devices. On 4.X and 6 Android it works fine. Can someone show me were is my mistake? Thank a lot!
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>
Interesting observation. When I scroll down my RecyclerView, on programm level its really scrolling! cause if I tap on item - it shows (in other fragment) item wich is on the bottom of list! And when I press "Back" it back me to the SCROLLED RecyclerView!!! I think it means the problem is near the Layout Manager, and concretely in Item Decorator or Item Animator.
I still trying to solve my problem, and look to the source code of ListView (package android.widget.ListView.java) And I found failed imports. For Example:
import com.google.android.collect.Lists;
import android.util.MathUtils;
import android.view.ViewRootImpl;
...and many others in other *.java files. Can it be connected with losing functionality of these components? And how can I fix this problem? Help me, please.
In your fragment you are implement the AdapterView.OnItemClick
I have already experienced this problem using click event the way you are using.
As you are using a RecyclerView, you need add a touch listener to the RecyclerView instead. Otherwise you may loose the scroll functionallity
This StackOverflow answer provides a better solution to get item feedback when using RecyclerView.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With