发表于: 2018-11-18 21:43:36

0 924


一、今天完成的事情

ToolBar

Toolbar 是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件。由于他的可定制度高,所以已经逐步替代掉了ActionBar。

先在活动中添加布局

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

为ToolBar写一个菜单布局

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/take_video"
android:title=""
android:icon="@drawable/outline_videocam_black_24"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/refresh"
android:title=""
android:icon="@drawable/outline_refresh_black_24"
app:showAsAction="ifRoom"/>
</menu>

在活动中使用ToolBar,并为其菜单子项设置点击事件,在使用Toolbar之前一定要去掉系统自带的ActionBar,可以给Activity使用NoActionBar主题,或者在setContentView()之前调用

/*
   初始化控件
    */
public void initView() {
drawerLayout = (DrawerLayout) findViewById(R.id.layout_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//actionBar的具体实现由toolBar完成
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
//设置导航图标,ToolBar最左侧的按钮名为HomeAsUp,原先默认图标为返回箭头,
// 默认作用是返回上一个活动,这里将它改为打开DrawerLayout
//必须先获取ToolBar实例再去设置图标,否则不起作用
actionBar.setHomeAsUpIndicator(R.drawable.outline_menu_black_24);
}
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
//设置默认选中的菜单
navigationView.setCheckedItem(R.id.nav_http);
navigationView.setNavigationItemSelectedListener(this);
addressCL = (ConstraintLayout) findViewById(R.id.cl_address);
addressCL.setVisibility(GONE);
glassView = (View) findViewById(R.id.view_glass);
glassView.setOnClickListener(this);
glassView.setVisibility(GONE);
addressET = (EditText) findViewById(R.id.et_address);
goBT = (Button) findViewById(R.id.bt_go_to_address);
goBT.setOnClickListener(this);
videoRV = (RecyclerView) findViewById(R.id.rv_video);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
videoRV.setLayoutManager(linearLayoutManager);
videoAdapter = new VideoAdapter(MainActivity.this, videoBeanList);
videoAdapter.setOnItemClickListener(this);
videoRV.setAdapter(videoAdapter);
progressBar = (ProgressBar) findViewById(R.id.bar_progress);
progressBar.setVisibility(GONE);
}

@Override
public void onItemClick(int position) {
Intent intent = new Intent(MainActivity.this, PlayVideoActivity.class);
intent.putExtra("source", videoBeanList.get(position).getPath());
startActivity(intent);
//JzvdStd.startFullscreen(this, JzvdStd.class, videoBeanList.get(position).getPath(), videoBeanList.get(position).getTitle());
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.nav_local:
break;
case R.id.nav_http:
if (glassView.getVisibility() == GONE) {
glassView.setVisibility(VISIBLE);
}
if (addressCL.getVisibility() == GONE) {
addressCL.setVisibility(VISIBLE);
}
drawerLayout.closeDrawers();
break;
case R.id.nav_history:
Toast.makeText(MainActivity.this, "History", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id.nav_setting:
Toast.makeText(MainActivity.this, "Setting", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id.nav_about:
Toast.makeText(MainActivity.this, "About", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
default:
break;
}
return true;
}


二、明天计划的事情

调用手机自带的加速计、陀螺仪、方向、磁场、温度、光线、压力等设备功能


三、遇到的问题    

在Fragment中更改Toolbar的Menu后,item无法响应点击事件

public class LocalVideoFragment extends Fragment implements VideoAdapter.OnItemClickListener {

   private static final String TAG = "LocalVideoFragment";

   private static final int NOTIFY_ADAPTER = 1;
   private static final int NO_VIDEO = 2;
   private static final int TAKE_VIDEO = 1;

   private List<VideoBean> videoBeanList = new ArrayList<VideoBean>();
   private static VideoAdapter videoAdapter;

   private MyHandler myHandler = new MyHandler();

   //private EditText addressET;
   private static ProgressBar progressBar;

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_local_video, container, false);
       initView(view);
       initVideo();
       return view;
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       //指明fragment愿意添加item到选项菜单(否则, fragment将接收不到对 onCreateOptionsMenu()的调用)
       setHasOptionsMenu(true);
   }

   @Override
   public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
       menu.clear();
       super.onCreateOptionsMenu(menu, inflater);
       inflater.inflate(R.menu.toolbar_menu, menu);
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
           case R.id.take_video:
               takeVideo();
               break;
           case R.id.refresh:
               initVideo();
               break;
           default:
       }
       return true;
   }


   @Override
   public void onItemClick(int position) {
       Intent intent = new Intent(getActivity(), PlayVideoActivity.class);
       intent.putExtra("source", videoBeanList.get(position).getPath());
       startActivity(intent);

       //JzvdStd.startFullscreen(this, JzvdStd.class, videoBeanList.get(position).getPath(), videoBeanList.get(position).getTitle());
   }

   /*
   初始化控件
    */
   public void initView(View view) {
       //addressET = (EditText) view.findViewById(R.id.et_address);
       //Button goBT = (Button) view.findViewById(R.id.bt_go_to_address);
       //goBT.setOnClickListener(this);
       RecyclerView videoRV = (RecyclerView) view.findViewById(R.id.rv_video);
       LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
       videoRV.setLayoutManager(linearLayoutManager);
       videoAdapter = new VideoAdapter(getContext(), videoBeanList);
       videoAdapter.setOnItemClickListener(this);
       videoRV.setAdapter(videoAdapter);
       progressBar = (ProgressBar) view.findViewById(R.id.bar_progress);
       progressBar.setVisibility(GONE);

   }


......



四、收获

   ToolBar、NavigationView


返回列表 返回列表
评论

    分享到