如何使用firebaseDatabase在tabLayout中实现recyclerView?

时间:2017-04-02 19:54:17

标签: android firebase firebase-realtime-database

FragmentClass

        public class BlankFragment extends Fragment {
               //FragmentClass constructor with no parameter
              public BlankFragment() {
              // Required empty public constructor
              }

             @Override
             public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);

             }


             **Inflating recyclerView with Dummy data i want to load data from firebase here


             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
              // Inflate the layout for this fragment


              View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
                //FInding recyclerViewByid
              RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
              rv.setHasFixedSize(true);
              // i have added dummy data into adapter but want toadd data from firebaseDatabase into Myadapter

              MyAdapter adapter = new MyAdapter(new String[] {
               "test one",  // i have hardcoded these data 
               "test two",  // but i want data from firebase to go here
               "test three",
               "test four",
               "test five",
               "test six",
               "test seven"
              });
              rv.setAdapter(adapter); //Setting adapter

              LinearLayoutManager llm = new LinearLayoutManager(getActivity());
              rv.setLayoutManager(llm);

              return rootView; // returning rootView
             }


            }



            MainActivity
            #MainActivity contains 3TabLayouts

            public class MainActivity extends AppCompatActivity {
             private RecyclerView recyclerView;
             private final String TAG = "SOMETHING";
             private ImageView image;




             protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              Firebase.setAndroidContext(this);
      //TabLayout
              TabLayout tabLayout = (TabLayout)findViewById(R.id.tab_layout);

              Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
              setSupportActionBar(toolbar);


              // Get the ViewPager and set it's PagerAdapter so that it can display items
              ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
              PagerAdapter pagerAdapter =
               new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
              viewPager.setAdapter(pagerAdapter);

              // Give the TabLayout the ViewPager
              tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
               @Override
               public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 1)
                 Log.d(TAG, "onTabSelected: "); //call some method
                else if (position == 2)
                 Log.d(TAG, "onTabSelected: "); //call some method
                else
                 Log.d(TAG, "onTabSelected: "); //call some method

               }

               @Override
               public void onTabUnselected(TabLayout.Tab tab) {

               }

               @Override
               public void onTabReselected(TabLayout.Tab tab) {

               }
              });




                //SettingViewpagers

              tabLayout.setupWithViewPager(viewPager);

              // Iterate over all tabs and set the custom view
              for (int i = 0; i < tabLayout.getTabCount(); i++) {
               TabLayout.Tab tab = tabLayout.getTabAt(i);
               tab.setCustomView(pagerAdapter.getTabView(i));
              }

             }


             @Override
             public void onResume() {
              super.onResume();
             }
             @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_main, menu);
              return true;
             }


             @Override
             public boolean onOptionsItemSelected(MenuItem item) {
              int id = item.getItemId();

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

              return super.onOptionsItemSelected(item);
             }

             class PagerAdapter extends FragmentPagerAdapter {

              String tabTitles[] = new String[] {
               "NOTICE",       // tab1 header
               "FEED",         // tab2 header  
               "RESULT"        // tab3 header
              };
              Context context;

              public PagerAdapter(FragmentManager fm, Context context) {
               super(fm);
               this.context = context;
              }



              @Override
              public int getCount() {
               return tabTitles.length;
              }

              @Override
              public Fragment getItem(int position) {
                // 3 tab classes

想在所有3个标签中加载不同的数据    所有数据都应该从firebase加载

               switch (position) {
                case 0:
                 return new BlankFragment(); //FragementClass 1

                case 1:
                 return new BlankFragment(); //FragmentClass 2

                case 2:
                 return new SomeFragment();  //FragmentClass 3
               }

               return null;
              }

              @Override
              public CharSequence getPageTitle(int position) {
                // Generate title based on item position
                return tabTitles[position];
               }
               // inflate some value should inflate some custom view here
              public View getTabView(int position) {
               View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_tab, null);
               TextView tv = (TextView) tab.findViewById(R.id.custom_text);
               tv.setText(tabTitles[position]);
               return tab;
              }


             }

            }


            #Adapter class

            import android.support.v7.widget.CardView;
            import android.support.v7.widget.RecyclerView;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;
            import android.view.animation.AlphaAnimation;
            import android.view.animation.Animation;
            import android.view.animation.ScaleAnimation;
            import android.widget.TextView;

            public class MyAdapter extends RecyclerView.Adapter < MyAdapter.MyViewHolder > {
             private String[] mDataset;
             private final static int FADE_DURATION = 1000;

             public MyAdapter() {

             }

             // 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 {
              public CardView mCardView;
              public TextView mTextView;
              public MyViewHolder(View v) {
               super(v);

               mCardView = (CardView) v.findViewById(R.id.card_view);
               mTextView = (TextView) v.findViewById(R.id.tv_text);
              }
             }

             // 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
              View v = LayoutInflater.from(parent.getContext())
               .inflate(R.layout.card_item, parent, false);
              // set the view's size, margins, paddings and layout parameters
              MyViewHolder vh = new MyViewHolder(v);
              return vh;
             }

             @Override
             public void onBindViewHolder(MyViewHolder holder, int position) {
              holder.mTextView.setText((CharSequence) mDataset[position]);
              setScaleAnimation(holder.mCardView);
              //  setFadeAnimation(holder.mTextView);
              ;
             }

//将简单的缩放动画添加到CardView

             private void setScaleAnimation(View view) {
              ScaleAnimation anim = new ScaleAnimation(0.0 f, 1.0 f, 0.0 f, 1.0 f, Animation.RELATIVE_TO_SELF, 0.5 f, Animation.RELATIVE_TO_SELF, 0.5 f);
              anim.setDuration(FADE_DURATION);
              view.startAnimation(anim);
             }

//添加淡入淡出动画

             private void setFadeAnimation(View view) {
              AlphaAnimation anim = new AlphaAnimation(0.0 f, 1.0 f);
              anim.setDuration(FADE_DURATION);
              view.startAnimation(anim);
             }
             @Override
             public int getItemCount() {
              return mDataset.length;
             }
            }

0 个答案:

没有答案