gpt4 book ai didi

android - RecyclerView 不滚动

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:56:47 26 4
gpt4 key购买 nike

我花了 4 天时间让我的 ListView 滚动,但我不能,然后有人建议使用 RecyclerView,因为我正在使用 CoordinatorLayout,所以我做了。现在在理解和实现 RecyclerView 之后仍然有同样的错误。它现在不滚动我该怎么办???我想放弃了。我无处可去。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contacts_list, container, false);
DataBaseOperations hell = new DataBaseOperations(getActivity());
SQLiteDatabase db = hell.getReadableDatabase();
String[] columns = {mDatabase.Tableinfo.ID,mDatabase.Tableinfo.Contacts_phone,mDatabase.Tableinfo.Contacts_name,mDatabase.Tableinfo.Contact_pic};
Cursor Contacts = db.query(mDatabase.Tableinfo.contacts, columns, null, null, null, null, null);
Toast.makeText(getContext(),"come on"+Contacts.getCount(),Toast.LENGTH_SHORT).show();
RecyclerView contacts = (RecyclerView) view.findViewById(R.id.contacts);
contacts.hasFixedSize();
RecyclerView.LayoutManager llv = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
contacts.setLayoutManager(llv);
Context t = getContext();
MyContactsRecyclerViewAdapter lv = new MyContactsRecyclerViewAdapter(Contacts,t);
contacts.setAdapter(lv);

return view;
}

这是我的 list.xml 的 xml,它在寻呼机中,我应该也发布它的代码吗????

<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/contacts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:scrollbars="vertical"
tools:listitem="@layout/fragment_contacts" />

如有任何帮助,我们将不胜感激。这是我的 MainActivty 的 xml。

<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=".mainactivity">

<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.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"

/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backscreen3"

/>


</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<!-- content_main doesn't really have anything. -->

</android.support.design.widget.CoordinatorLayout>

这是我的 RecyclerAdapter。

public class MyContactsRecyclerViewAdapter extends RecyclerView.Adapter<MyContactsRecyclerViewAdapter.ViewHolder> {

private Cursor cursor;
private Context mcontext;
private View view;
public MyContactsRecyclerViewAdapter(Cursor cur, Context con){
cursor = cur;
mcontext = con;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_contacts, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
cursor.moveToPosition(position);
String nam = cursor.getString(cursor.getColumnIndex(mDatabase.Tableinfo.Contacts_name));
String phon = cursor.getString(cursor.getColumnIndex(mDatabase.Tableinfo.Contacts_phone));
byte[] b = cursor.getBlob(cursor.getColumnIndex(mDatabase.Tableinfo.Contact_pic));
holder.name.setText(nam);
holder.phone.setText(phon);
if (b==null) {
Profile bla = new Profile();
holder.pic.setImageBitmap(bla.getRoundedShape(BitmapFactory.decodeResource(mcontext.getResources(), R.drawable.samplepro),60,60));
} else {
holder.pic.setImageBitmap(BitmapFactory.decodeByteArray(b,0,b.length));
}
}


@Override
public int getItemCount() {
if(cursor != null){
return cursor.getCount();}
return 0 ;
}


public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public TextView name;
public TextView phone;
public ImageView pic;

public ViewHolder(View view) {
super(view);
mView = view;
pic = (ImageView) view.findViewById(R.id.image);
phone = (TextView) view.findViewById(R.id.phone);
name = (TextView)view.findViewById(R.id.name);

}}}

就是这样。我认为每件事都与之相关。请查找错误。记住 ScrollBars 来了又消失了,但仍然没有滚动

最佳答案

看来您在 <include layout="@layout/content_main" /> 中遇到了问题 有了那个ViewPager .(正如我在 android studio 预览中看到的那样)

像这样使用它:

<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="com.example.umerasif.chattris.Chattris">


<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme">


<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="470dp"
android:layout_weight="1"
android:background="@android:color/darker_gray"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.AppBarLayout>

它现在应该可以工作了。

关于android - RecyclerView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34729128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com