简体   繁体   English

Recycleview不显示项目

[英]Recycleview doesn't show the items

My APP is composed by 4 fragments, 1 activity (mainpage). 我的APP由4个片段,1个活动(主页)组成。 Each fragment show somethings; 每个片段都显示出一些东西。 in this case I want to show to the user some informations about telecommunications, but i really need to use a RecycleView. 在这种情况下,我想向用户显示一些有关电信的信息,但是我确实需要使用RecycleView。

If I try to put a TextView, it works fine, but with a ListView or a RecycleView it doesn't show nothing. 如果我尝试放置TextView,它可以正常工作,但是使用ListView或RecycleView时,它什么也不显示。

homepage.java homepage.java

import ...

public class homepage extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homepage);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new 
SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new 
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new 
TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_homepage, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = null;

        switch(getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1:
                rootView = inflater.inflate(R.layout.fragment_homepage, 
container, false);
                break;
            case 2:
                rootView = inflater.inflate(R.layout.fragment_fibra, 
container, false);
                break;
            case 3:
                rootView = inflater.inflate(R.layout.fragment_fibra, 
container, false);
                break;
            case 4:
                rootView = inflater.inflate(R.layout.fragment_aiuto, 
container, false);
                break;
        }
        return rootView;
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 4;
    }
}
}

fragment_fibra.xml fragment_fibra.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fibra"
android:background="#EFEFEF">

    <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/AppBarOverlay">

    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/ListaFibra"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:visibility="visible" />

</FrameLayout>

MyAdapter.java MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> 
{
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public MyViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        // create a new view
        TextView v = (TextView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_homepage, parent, false);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

You are never using the recyclerview. 您从未使用过recyclerview。

You can do the following things in the onCreateView method of your Fragment : 您可以在FragmentonCreateView方法中执行以下操作:

  • Create a list of items to pass to your RecyclerView: ArrayList items = new ArrayList<>(); 创建要传递到RecyclerView的项目列表: ArrayList items = new ArrayList <>();
  • Don't forget to fill previous list with some items. 别忘了用某些项目填充先前的列表。
  • Get an instance of your RecyclerView: RecyclerView recyclerView = findViewById(R.id.ListaFibra); 获取您的RecyclerView的实例: RecyclerView recyclerView = findViewById(R.id.ListaFibra);
  • Set LayoutManager: recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 设置LayoutManager: recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  • Get an instance of your adapter: MyAdapter adapter = new MyAdapter(this, items); 获取适配器的实例: MyAdapter adapter = new MyAdapter(this,items);
  • Set adapter on your RecylcerView: recyclerView.setAdapter(adapter); 在RecylcerView上设置适配器: recyclerView.setAdapter(adapter);

That could be a starting point. 那可能是一个起点。 Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM