Android系统。棒糖。 RecyclerView不会滚动

时间:2016-01-15 17:30:15

标签: android android-fragments android-recyclerview scrollable

我试图在我的应用程序中制作RecyclerView,但我无法使其可滚动。这里讨论了很多相同的问题,但没有解决方案可以帮助我。 我阅读了手册: http://developer.android.com/training/material/lists-cards.html#RVExamples 我尝试在Fragment中做到这一点。问题是当我尝试在我的适配器中滚动RecView,LayoutManager调用onBindViewHolder()和getItemCount()方法时,但在视觉上没有任何反应。仅适用于棒棒糖设备。在4.X和6 Android上它运行正常。 有人能告诉我这是我的错吗? 非常感谢!

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);
    }
}

片段:

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时,在程序级别它真的滚动!因为如果我点击项目 - 它显示(在其他片段中)项目位于列表的底部!当我按下&#34;返回&#34;它让我回到SCROLLED 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文件中的许多其他文件。 是否可以与这些组件的功能丢失相关联?我该如何解决这个问题呢?请帮帮我。

3 个答案:

答案 0 :(得分:2)

在您的片段中,您实现AdapterView.OnItemClick我已经使用您使用的方式使用点击事件来解决此问题。

在使用RecyclerView时,需要向RecyclerView添加触摸侦听器。否则你可能会失去滚动功能

使用RecyclerView时,

This StackOverflow answer为获取项目反馈提供了更好的解决方案。

答案 1 :(得分:0)

在您的RecyclerView中添加这一行,它有望工作。

     app:layout_behavior="@string/appbar_scrolling_view_behavior"

答案 2 :(得分:-1)

尝试删除FrameLayout