want to display data in listview on fragment, but can only display a listview background color
the location of the navigation bar should show the news data below, but it is empty.
mainactivity Code
public class MainActivity extends FragmentActivity {
private TabLayout tab_title;
private ViewPager vp_pager;
private List<String> list_title; //tab
private List<View> listViews;
private View newsView; //
private View sportView; //
private View funView; //
private View science; //
private viewAdapter vAdapter; //viewadapter
private final static String TAG="MainActivity";
private int[] tabImg;
private ArrayList<NewsBean> newsBeans =null;
private DrawerLayout drawerLayout;
private RelativeLayout rightLayout;
private ImageView leftMenu, rightMenu;
private ListView mListView;
private List<ContentModel> mList;
private ArrayList<Data> datas = null;
private FragmentManager fManager = null;
private long exitTime = 0;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
//title
requestWindowFeature(Window.FEATURE_NO_TITLE);
// if (getSupportActionBar() != null){
// getSupportActionBar().hide();
// }
// setContentView(R.layout.activity_main);
setContentView(R.layout.main);
mContext = this;
//1.
ListView lv_news = findViewById(R.id.lv_news);
System.out.println("lv_show");
//2.list
ArrayList<NewsBean> allNews = NewsUtils.getAllNews(mContext);
NewsBean a = allNews.get(0);
System.out.println("newsbean:PPPP"+a);
//3.adapterlistview
NewsAdapter newsAdapter = new NewsAdapter(mContext, allNews);
lv_news.setAdapter(newsAdapter);
//4.listview
//lv_news.setOnItemClickListener(this);
//
initControls();
viewChanage();
initData();
leftMenu = (ImageView) findViewById(R.id.leftmenu);
rightMenu = (ImageView) findViewById(R.id.rightmenu);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
rightLayout = (RelativeLayout) findViewById(R.id.right);
mListView = (ListView) findViewById(R.id.left_listview);
System.out.println("");
ContentAdapter adapter = new ContentAdapter(this,mList);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ContentModel contentModel = (ContentModel) parent.getItemAtPosition(position);
int i = contentModel.getId();
switch (i){
case 1:
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
break;
case 2:
// Intent intent = new Intent(MainActivity.this,LoginActivity.class);
// startActivity(intent);
Toast.makeText(MainActivity.this,"",Toast.LENGTH_LONG).show();
break;
case 3:
// Intent intent = new Intent(MainActivity.this,LoginActivity.class);
// startActivity(intent);
Toast.makeText(MainActivity.this,"",Toast.LENGTH_LONG).show();
break;
case 4:
// Intent intent = new Intent(MainActivity.this,LoginActivity.class);
// startActivity(intent);
Toast.makeText(MainActivity.this,"",Toast.LENGTH_LONG).show();
break;
}
// FragmentTransaction bt = fm.beginTransaction();
// switch ((int) id) {
// case 1:
// //bt.replace(R.id.content, new NewsFragment());
//
// Intent intent = new Intent(MainActivity.this,LoginActivity.class);
// startActivity(intent);
//
// break;
// case 2:
// bt.replace(R.id.content, new SubscriptionFragment());
// break;
//
// default:
// break;
// }
//bt.commit();
//drawerLayout.closeDrawer(Gravity.LEFT);
}
});
leftMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"",Toast.LENGTH_LONG).show();
drawerLayout.openDrawer(Gravity.LEFT);
}
});
rightMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.RIGHT);
}
});
rightLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.closeDrawer(Gravity.RIGHT);
}
});
// //1.Fragment
// FragmentManager fragmentManager = getFragmentManager();
// //2.
// FragmentTransaction transaction = fragmentManager.beginTransaction();
// //3.
// FragmentTransaction replace = transaction.replace(R.id.lv_tt, new NewsFragment());
// //4.commit
// transaction.commit();
}
/**
*
*/
private void initControls()
{
//title
tab_title = (TabLayout)findViewById(R.id.tab_title);
vp_pager = (ViewPager)findViewById(R.id.vp_pager);
//initBottomNavigationBar();
//bottom
//tabLayout
tabImg = new int[]{R.drawable.icon_three,R.drawable.icon_one,R.drawable.icon_two ,R.drawable.icon_four};
}
/**
* viewpager view ,icon
*/
private void viewChanage()
{
listViews = new ArrayList<>();
LayoutInflater mInflater = getLayoutInflater();
//title
newsView = mInflater.inflate(R.layout.fragment_news, null);
sportView = mInflater.inflate(R.layout.fragment_sports, null);
funView = mInflater.inflate(R.layout.fragment_amusement, null);
science = mInflater.inflate(R.layout.fragment_science,null);
listViews.add(newsView);
listViews.add(sportView);
listViews.add(funView);
listViews.add(science);
list_title = new ArrayList<>();
list_title.add("");
list_title.add("");
list_title.add("");
list_title.add("");
//TabLayout,tab
//TabLayout.MODE_FIXED tab,
//TabLayout.MODE_SCROLLABLE; // tabtab
//
tab_title.setTabMode(TabLayout.MODE_FIXED); //
// tab_title.setTabMode(TabLayout.MODE_SCROLLABLE);
//tablayout
//tab_title.setPadding(20,20,20,20);
//TabLayouttab
tab_title.addTab(tab_title.newTab().setText(list_title.get(0)));
tab_title.addTab(tab_title.newTab().setText(list_title.get(1)));
tab_title.addTab(tab_title.newTab().setText(list_title.get(2)));
tab_title.addTab(tab_title.newTab().setText(list_title.get(3)));
vAdapter = new viewAdapter(this,listViews,list_title,tabImg);
vp_pager.setAdapter(vAdapter);
//tabLayoutviewpager
tab_title.setupWithViewPager(vp_pager);
}
private void initData() {
mList = new ArrayList<ContentModel>();
mList.add(new ContentModel(R.drawable.icon_one, "", 1));
mList.add(new ContentModel(R.drawable.icon_two, "", 2));
mList.add(new ContentModel(R.drawable.icon_three, "", 3));
mList.add(new ContentModel(R.drawable.icon_four, "", 4));
mList.add(new ContentModel(R.drawable.icon_one, "", 5));
mList.add(new ContentModel(R.drawable.icon_four, "", 6));
}
}
Fragment section:
public class NewsFragment extends Fragment {
private ListView lv_news;
private ListView list;
private SimpleAdapter adapter;
protected Context mContext;
// @Override
// public void onAttach(Activity activity) {
// super.onAttach(activity);
// this.mActivity = activity;
// }
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private ListView ListView;
//uiFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//view
//View rootview=inflater.inflate(R.layout.fragment_news,container,false);
View view = LayoutInflater.from(this.getActivity()).inflate(R.layout.fragment_news, null);
//4.listview
lv_news.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});*/
//1.
ListView lv_news = (ListView) view.findViewById(R.id.lv_newss);
System.out.println("1111111");
//2.list
ArrayList<NewsBean> allNews = NewsUtils.getAllNews(getActivity());
System.out.println("newsbean");
//3.adapterlistview
NewsAdapter newsAdapter = new NewsAdapter(getActivity(),allNews);
lv_news.setAdapter(newsAdapter);
//4.listview
//lv_news.setOnItemClickListener(this);
return view;
}
@Override
public void onDetach() {
super.onDetach();
}
}
main layout xml
<android.support.design.widget.TabLayout
android:id="@+id/tab_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary"
app:tabIndicatorColor="@color/colorAccent"
app:tabSelectedTextColor="@color/blue_light"
app:tabTextColor="@color/material_orange" />
<!--app:tabTextAppearance="@style/tablayoutIcon"-->
<android.support.v4.view.ViewPager
android:id="@+id/vp_pager"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1" />
fragment layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv_newss"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="@color/colorPrimary">
</ListView>
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv_newss"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="@color/colorPrimary">
</ListView>
</LinearLayout>