80,349
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<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" />
</android.support.design.widget.AppBarLayout>
<!-- 内容区 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:layout_marginBottom="61dp">
<SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
android:queryHint="搜索单词" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true">
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<Button
android:id="@+id/word_btn_hide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/button_selector"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="@string/review_btn_hide"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="@+id/word_btn_back"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="@id/guideline"
app:layout_constraintRight_toRightOf="parent"
style="?android:attr/borderlessButtonStyle" />
<Button
android:id="@+id/word_btn_back"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/button_selector"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="@string/review_btn_back"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/guideline"
style="?android:attr/borderlessButtonStyle" />
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
public class TestBean {
private String name;
public TestBean(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder>{
private List<TestBean> mList;
static class ViewHolder extends RecyclerView.ViewHolder{
TextView name;
public ViewHolder(View view){
super(view);
name = (TextView)view.findViewById(R.id.test_name);
}
}
public TestAdapter(List<TestBean> strList){
mList = strList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.string_item,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
TestBean name = mList.get(position);
holder.name.setText(name.getName());
}
@Override
public int getItemCount() {
return mList.size();
}
}
public class WordActivity extends BaseActivity {
Toolbar toolbar;
ActionBar actionBar;
private List<TestBean> nameList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word);
toolbar = (Toolbar) this.findViewById(R.id.toolbar);
setSupportActionBar(toolbar); //使用Toolbar
actionBar = getSupportActionBar(); //获取ActionBar实例,具体实现由Toolbar完成
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true); //显示导航按钮设置为true
}
//返回按钮点击事件
Button button_back = (Button) findViewById(R.id.word_btn_back);
button_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(WordActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
//RecyclerView测试区
initNames();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_recycler);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
TestAdapter adapter = new TestAdapter(nameList);
recyclerView.setAdapter(adapter);
}
private void initNames() {
for(int i = 0;i < 40;i++){
TestBean testBean = new TestBean("---TestData--- " + i);
nameList.add(testBean);
}
}
//加载toolbar文件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//加载toolbar布局
getMenuInflater().inflate(R.menu.word_activity_toolbar, menu);
return true;
}
//为按钮设置点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.translate_bar_add:
Toast.makeText(this, "添加到单词本\n功能未完成", Toast.LENGTH_SHORT).show();
break;
case android.R.id.home:
//前往主界面
Intent intent = new Intent(WordActivity.this,MainActivity.class);
startActivity(intent);
finish();
break;
default:
}
return true;
}
}