完成了主界面的布局
package com.hui.testend;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentContainerView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.hui.testend.community.addcontent;
import com.hui.testend.community.addcontent01;
import com.hui.testend.fragment.Fragment_community;
import com.hui.testend.fragment.Fragment_home;
import com.hui.testend.fragment.Fragment_self;
import com.hui.testend.list.ItemBean;
import com.hui.testend.list.MyAdapter;
import com.hui.testend.mytool.Dao;
import com.hui.testend.recongnize.baiduapi;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ListView list;
FragmentContainerView fcv;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
Handler mainhandler;
LinearLayout ll_home,ll_community,ll_self;
ImageView iv_home,iv_community,iv_self;
TextView tv_home,tv_community,tv_self;
Dao dao=new Dao();
Toolbar toolbar;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shequ);
toolbar =(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init();
//initdata();
initevent();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemid= item.getItemId();
switch(itemid){
case R.id.menu_main:
Intent intent=new Intent(this, MainActivity.class);
startActivity(intent);
break;
case R.id.menu_add_comm:
Intent intent1=new Intent(this, addcontent01.class);
startActivity(intent1);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
setselector();
switch(v.getId()){
case R.id.ll_home:
toolbar.setTitle("主界面");
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
Fragment_home fragmentHome=Fragment_home.newInstance("主界面","");
fragmentTransaction.replace(R.id.fcv,fragmentHome).commit();
iv_home.setSelected(true);
tv_home.setTextColor(ContextCompat.getColor(this,R.color.green_700));
break;
case R.id.ll_community:
toolbar.setTitle("社区");
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
Fragment_community fragmentHome1=Fragment_community.newInstance("主界面1","");
fragmentTransaction.replace(R.id.fcv,fragmentHome1).commit();
iv_community.setSelected(true);
tv_community.setTextColor(ContextCompat.getColor(this,R.color.green_700));
break;
case R.id.ll_self:
toolbar.setTitle("个人信息");
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
Fragment_self fragmentSelf=Fragment_self.newInstance("主界面2","");
fragmentTransaction.replace(R.id.fcv,fragmentSelf).commit();
iv_self.setSelected(true);
tv_self.setTextColor(ContextCompat.getColor(this,R.color.green_700));
break;
}
}
public void setselector(){
iv_home.setSelected(false);
iv_community.setSelected(false);
iv_self.setSelected(false);
tv_home.setTextColor(ContextCompat.getColor(this, R.color.gray));
tv_community.setTextColor(ContextCompat.getColor(this, R.color.gray));
tv_self.setTextColor(ContextCompat.getColor(this, R.color.gray));
}
public void initdata(){
new Thread(new Runnable() {
@Override
public void run() {
List<ItemBean> listdata=new ArrayList<>();
listdata=dao.getmain_data();
List<ItemBean> finalListdata = listdata;
mainhandler.post(new Runnable() {
@Override
public void run() {
myAdapter=new MyAdapter(MainActivity.this, finalListdata);
list.setAdapter(myAdapter);
get(finalListdata);
}
});
}
}).start();
}
public void get(List<ItemBean> a){
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent =new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://baike.baidu.com/item/"+a.get(position).getTitle()));
startActivity(intent);
}
});
}
public void initevent(){
toolbar.setTitle("主界面");
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment_home fragmentHome=Fragment_home.newInstance("主界面","");
fragmentTransaction.replace(R.id.fcv,fragmentHome).commit();
iv_home.setSelected(true);
tv_home.setTextColor(ContextCompat.getColor(this,R.color.green_700));
}
public void init(){
ll_home=findViewById(R.id.ll_home);
ll_community=findViewById(R.id.ll_community);
ll_self=findViewById(R.id.ll_self);
ll_home.setOnClickListener(this);
ll_community.setOnClickListener(this);
ll_self.setOnClickListener(this);
fcv=findViewById(R.id.fcv);
iv_home=findViewById(R.id.iv_home);
iv_community=findViewById(R.id.iv_community);
iv_self=findViewById(R.id.iv_self);
tv_home=findViewById(R.id.tv_home);
tv_community=findViewById(R.id.tv_community);
tv_self=findViewById(R.id.tv_self);
//list=findViewById(R.id.list1);
mainhandler=new Handler(getMainLooper());
}
}
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentContainerView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.hui.testend.community.addcontent;
import com.hui.testend.community.addcontent01;
import com.hui.testend.fragment.Fragment_community;
import com.hui.testend.fragment.Fragment_home;
import com.hui.testend.fragment.Fragment_self;
import com.hui.testend.list.ItemBean;
import com.hui.testend.list.MyAdapter;
import com.hui.testend.mytool.Dao;
import com.hui.testend.recongnize.baiduapi;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ListView list;
FragmentContainerView fcv;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
Handler mainhandler;
LinearLayout ll_home,ll_community,ll_self;
ImageView iv_home,iv_community,iv_self;
TextView tv_home,tv_community,tv_self;
Dao dao=new Dao();
Toolbar toolbar;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shequ);
toolbar =(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init();
//initdata();
initevent();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemid= item.getItemId();
switch(itemid){
case R.id.menu_main:
Intent intent=new Intent(this, MainActivity.class);
startActivity(intent);
break;
case R.id.menu_add_comm:
Intent intent1=new Intent(this, addcontent01.class);
startActivity(intent1);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
setselector();
switch(v.getId()){
case R.id.ll_home:
toolbar.setTitle("主界面");
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
Fragment_home fragmentHome=Fragment_home.newInstance("主界面","");
fragmentTransaction.replace(R.id.fcv,fragmentHome).commit();
iv_home.setSelected(true);
tv_home.setTextColor(ContextCompat.getColor(this,R.color.green_700));
break;
case R.id.ll_community:
toolbar.setTitle("社区");
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
Fragment_community fragmentHome1=Fragment_community.newInstance("主界面1","");
fragmentTransaction.replace(R.id.fcv,fragmentHome1).commit();
iv_community.setSelected(true);
tv_community.setTextColor(ContextCompat.getColor(this,R.color.green_700));
break;
case R.id.ll_self:
toolbar.setTitle("个人信息");
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
Fragment_self fragmentSelf=Fragment_self.newInstance("主界面2","");
fragmentTransaction.replace(R.id.fcv,fragmentSelf).commit();
iv_self.setSelected(true);
tv_self.setTextColor(ContextCompat.getColor(this,R.color.green_700));
break;
}
}
public void setselector(){
iv_home.setSelected(false);
iv_community.setSelected(false);
iv_self.setSelected(false);
tv_home.setTextColor(ContextCompat.getColor(this, R.color.gray));
tv_community.setTextColor(ContextCompat.getColor(this, R.color.gray));
tv_self.setTextColor(ContextCompat.getColor(this, R.color.gray));
}
public void initdata(){
new Thread(new Runnable() {
@Override
public void run() {
List<ItemBean> listdata=new ArrayList<>();
listdata=dao.getmain_data();
List<ItemBean> finalListdata = listdata;
mainhandler.post(new Runnable() {
@Override
public void run() {
myAdapter=new MyAdapter(MainActivity.this, finalListdata);
list.setAdapter(myAdapter);
get(finalListdata);
}
});
}
}).start();
}
public void get(List<ItemBean> a){
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent =new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://baike.baidu.com/item/"+a.get(position).getTitle()));
startActivity(intent);
}
});
}
public void initevent(){
toolbar.setTitle("主界面");
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment_home fragmentHome=Fragment_home.newInstance("主界面","");
fragmentTransaction.replace(R.id.fcv,fragmentHome).commit();
iv_home.setSelected(true);
tv_home.setTextColor(ContextCompat.getColor(this,R.color.green_700));
}
public void init(){
ll_home=findViewById(R.id.ll_home);
ll_community=findViewById(R.id.ll_community);
ll_self=findViewById(R.id.ll_self);
ll_home.setOnClickListener(this);
ll_community.setOnClickListener(this);
ll_self.setOnClickListener(this);
fcv=findViewById(R.id.fcv);
iv_home=findViewById(R.id.iv_home);
iv_community=findViewById(R.id.iv_community);
iv_self=findViewById(R.id.iv_self);
tv_home=findViewById(R.id.tv_home);
tv_community=findViewById(R.id.tv_community);
tv_self=findViewById(R.id.tv_self);
//list=findViewById(R.id.list1);
mainhandler=new Handler(getMainLooper());
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/green_700"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fcv"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/gray"/>
<include layout="@layout/bottom_btn_nav"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/green_700"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fcv"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/gray"/>
<include layout="@layout/bottom_btn_nav"/>
</LinearLayout>