Question: Android Studio help with ViewPager and ListView. I created ViewPager and the ListView but I don't know how to put it together with Listview like
Android Studio help with ViewPager and ListView. I created ViewPager and the ListView but I don't know how to put it together with Listview like the one in the Demo video. Link for Demo below. Thanks
https://www.youtube.com/watch?v=B7csDJQqte8
My code for ViewPagerActivity.java
public class ViewPagerActivity extends AppCompatActivity { private ViewPager viewPager; private ArrayList list = new ArrayList(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_pager); Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show(); Log.d("LifeCycle", "onCreate"); viewPager = (ViewPager)findViewById(R.id.activity_view_pager); list.add(new RedFragment()); list.add(new GreenFragment()); list.add(new BlueFragment()); BaseViewPagerAdapter pagerAdapter = new BaseViewPagerAdapter(getSupportFragmentManager(),list); viewPager.setAdapter(pagerAdapter); viewPager.setCurrentItem(0); } public ViewPagerActivity() { super(); } @Override protected void onStart() { super.onStart(); Log.d("LifeCycle", "onStart"); } @Override protected void onResume() { super.onResume(); Log.d("LifeCycle", "onResume"); } @Override protected void onPause() { super.onPause(); Log.d("LifeCycle", "onPause"); } @Override protected void onStop() { super.onStop(); Log.d("LifeCycle", "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d("LifeCycle", "onDestroy"); } @Override protected void onRestart() { super.onRestart(); Log.d("LifeCycle", "onRestart"); } } My code for UtilLog.java below
public class UtilLog { private static boolean isLog = false; //static means one instance or value //create method to control log variable public static void setIsLog(boolean b){ isLog = b; } //static while using class log public static void d(String tag, String msg){ if(isLog){ Log.d(tag, msg); } } public static void v(String tag, String msg){ if(isLog){ Log.v(tag, msg); //to create more than one method } } } My code for BlueFragment.java below (GreenFragment.java and RedFragment.java similar as BlueFragment.java so I won't paste below)
public class BlueFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; public BlueFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment BlueFragment. */ // TODO: Rename and change types and number of parameters public static BlueFragment newInstance(String param1, String param2) { BlueFragment fragment = new BlueFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } UtilLog.d("Fragment", "BlueOnCreate"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_blue, container, false); } @Override public void onStart() { super.onStart(); UtilLog.d("Fragment", "Blue:onStart"); } @Override public void onResume() { super.onResume(); UtilLog.d("Fragment", "Blue:onResume"); } @Override public void onPause() { super.onPause(); UtilLog.d("Fragment", "Blue:onPause"); } @Override public void onStop() { super.onStop(); UtilLog.d("Fragment", "Blue:onStop"); } @Override public void onDestroy() { super.onDestroy(); UtilLog.d("Fragment", "Blue:onDestroy"); } } My code for the listview AdvanceListViewActivity.java below
public class AdvanceListViewActivity extends BaseActivity { @BindView(R.id.activity_advance_list_view) ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_advance); ButterKnife.bind(this); ArrayList list = new ArrayList<>(); list.add("Hello"); list.add("Hello, how are you?"); list.add("I am fine, thank you!"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); list.add("Hello"); AdvanceListViewAdapter adapter = new AdvanceListViewAdapter(this, list); lv.setAdapter(adapter); TextView tv = new TextView(this); tv.setText("HeaderView"); tv.setTextSize(50); lv.addHeaderView(tv); TextView tv1 = new TextView(this); tv1.setText("FooterView"); tv1.setTextSize(50); lv.addFooterView(tv1); lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView> parent, View view, int position, long id){ showToast(String.valueOf(position)); } }); } } My code for the BaseActivity.java below
public class BaseActivity extends AppCompatActivity { public void showToast(String s){ Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); } public void shortToast(String s){ Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); } public void longToast(String s){ Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); } public void goToActivity(Class c){ Intent intent = new Intent(this, c); startActivity(intent); } } And my XML code for activity_advance.xml below
<LinearLayout 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:orientation="vertical" <ListView android:id="@+id/activity_advance_list_view" android:layout_width="match_parent" android:layout_height="match_parent">ListView> LinearLayout>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
