我使用了android工作室中的Bottom Navigation Activity。 在MainActivity中,我创建了一个列表视图(用户可以将其更改为网格视图),该视图应该位于home项目中。当我运行项目时,主页项目中没有内容
在MainActivity中
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private ViewStub stubGrid;
private ViewStub stubList;
private ListView listView;
private GridView gridView;
public ListViewAdapter listViewAdapter;
public GridViewAdapter gridViewAdapter;
public List<Product> productList;
private int currentViewMode = 0;
static final int VIEW_MODE_LISTVIEW = 1;
static final int VIEW_MODE_GRIDVIEW = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stubList = (ViewStub) findViewById(R.id.stub_list);
stubGrid = (ViewStub) findViewById(R.id.stub_grid);
//Inflate ViewStub before get view
stubList.inflate();
stubGrid.inflate();
listView = (ListView) findViewById(R.id.myListView);
gridView = (GridView) findViewById(R.id.myGridView);
//get list of product
getProductList();
//Get current view mode in share reference
SharedPreferences sharedPreferences = getSharedPreferences("ViewMode",
MODE_PRIVATE);
currentViewMode = sharedPreferences.getInt("currentViewMode",
VIEW_MODE_LISTVIEW);//Default is view listview
//Register item lick
listView.setOnItemClickListener(onItemClick);
gridView.setOnItemClickListener(onItemClick);
switchView();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private void switchView() {
if(VIEW_MODE_LISTVIEW == currentViewMode) {
//Display listview
stubList.setVisibility(View.VISIBLE);
//Hide gridview
stubGrid.setVisibility(View.GONE);
} else {
//Hide listview
stubList.setVisibility(View.GONE);
//Display gridview
stubGrid.setVisibility(View.VISIBLE);
}
setAdapters();
}
private void setAdapters() {
if(VIEW_MODE_LISTVIEW == currentViewMode) {
listViewAdapter = new ListViewAdapter(this, R.layout.list_item,
productList);
listView.setAdapter(listViewAdapter);
} else {
gridViewAdapter = new GridViewAdapter(this, R.layout.grid_item,
productList);
gridView.setAdapter(gridViewAdapter);
}
}
public List<Product> getProductList() {
//pseudo code to get product, replace your code to get real product here
productList = new ArrayList<>();
productList.add(new Product(R.drawable.ii1, " Story name","http://www.storiesrealistic.com","","1"));
productList.add(new Product(R.drawable.i2, "اStory name","http://www.cairodar.com/542427/","","2"));
productList.add(new Product(R.drawable.i3, "Story name","http://www.kids.jo/","","3"));
productList.add(new Product(R.drawable.i4, "Story name","http://mawdoo3.com/","","4"));
productList.add(new Product(R.drawable.i5, "Story name","http://mawdoo3.com/","","5"));
productList.add(new Product(R.drawable.i13, "Story name","http://mawdoo3.com/","","6"));
productList.add(new Product(R.drawable.i8, "Story name","http://kids.almo7eb.com/","","7"));
productList.add(new Product(R.drawable.i9, "Story name","http://www.storiesrealistic.com/","","8"));
productList.add(new Product(R.drawable.i7, "Story name","http://www.storiesrealistic.com/","","9"));
productList.add(new Product(R.drawable.i10, "Story name","https://www.roro44.net/90107/","","10"));
productList.add(new Product(R.drawable.i11, "Story name","http://www.storiesrealistic.com/","","11"));
productList.add(new Product(R.drawable.i12, "Story name","http://mawdoo3.com/","","12"));
productList.add(new Product(R.drawable.i6, "Story name","http://www.storiesrealistic.com/","","13" ));
return productList;
}
AdapterView.OnItemClickListener onItemClick = new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Do any thing when user click to item
Toast.makeText(getApplicationContext(),
productList.get(position).getTitle(),
Toast.LENGTH_SHORT).show();
Intent i= new Intent(MainActivity.this,SecondActivity.class);
Bundle b= new Bundle();
b.putString("Story Name", productList.get(position).getTitle());
b.putInt("Story Image",productList.get(position).getImageId());
b.putString("Story Title",productList.get(position).getTitle());
b.putString("Source", productList.get(position).getSource());
b.putString("Genres", productList.get(position).getGenres());
b.putString("Reader", productList.get(position).getRead());
i.putExtras(b);
startActivity(i);
}
};
BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
return true;
case R.id.navigation_fav:
return true;
case R.id.navigation_share:
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
String shareBody="Your Body Here";
String shareSub="Your Subject Here";
myIntent.putExtra(Intent.EXTRA_SUBJECT,shareSub);
myIntent.putExtra(Intent.EXTRA_TEXT,shareBody);
startActivity(Intent.createChooser(myIntent,"Share using"));
return false;
case R.id.nav_Rating:
return true;
case R.id.navigation_send:
Intent chooser= null;
myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setData(Uri.parse("Mail To:"));
myIntent.putExtra(Intent.EXTRA_EMAIL,new String[]{"norry-7@hotmail.com"});
myIntent.setType("message/rfc822");
chooser= Intent.createChooser(myIntent,"Send Email");
startActivity(chooser);
return false;
}
return false;
}
};
}
分享和发送项目工作正常,但在家里项目没有任何显示。我在MainActivity中的列表视图代码
setContentView
在activity_main