我使用 viewpager 来显示每月的数据。我想更改折叠工具栏的标题,以更改 viewpager 的页面。折叠工具栏标题仅在向上滚动工具栏时更改。
我的布局
<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.support.v4.widget.NestedScrollView
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="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="@dimen/activity_history_view_pager_height" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:id="@+id/my_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/activity_history_collapsing_toolbar_height"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_flexible_space"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
<TextView
android:id="@+id/txt_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_history_margin_left"
android:layout_marginStart="@dimen/activity_history_margin_left"
android:text="@string/activity_history_year"
android:textColor="@color/white"
android:textSize="@dimen/activity_history_text_size" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_history_margin_bottom"
android:orientation="vertical"
app:layout_scrollFlags="scroll">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="@dimen/activity_history_margin_right"
android:layout_marginRight="@dimen/activity_history_margin_right"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_leftBackArrow"
android:layout_width="@dimen/activity_history_arrow_size"
android:layout_height="@dimen/activity_history_arrow_size"
android:contentDescription="@string/content_description"
android:src="@drawable/previous_arrow" />
<ImageView
android:id="@+id/img_rightBackArrow"
android:layout_width="@dimen/activity_history_arrow_size"
android:layout_height="@dimen/activity_history_arrow_size"
android:layout_marginLeft="@dimen/activity_history_arrow_size"
android:layout_marginStart="@dimen/activity_history_arrow_size"
android:contentDescription="@string/content_description"
android:src="@drawable/next_arrow" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
活动中
/**
* Lists History of the Tasks and it's status on the basis of Date
*/
public class HistoryActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
@InjectView(R.id.toolbar_flexible_space)
Toolbar toolbar;
@InjectView(R.id.collapsing_toolbar)
CollapsingToolbarLayout mCollapsingToolbar;
@InjectView(R.id.view_pager)
ViewPager viewPager;
@InjectView(R.id.txt_year)
TextView txtYear;
int viewPagerSize = 0;
ArrayList<String> taskAddedMonths;
@OnClick({R.id.img_leftBackArrow, R.id.img_rightBackArrow})
public void onClick(View v) {
switch (v.getId()) {
case R.id.img_rightBackArrow:
moveForward();
break;
case R.id.img_leftBackArrow:
moveBackWard();
break;
}
}
/**
* move view pager page as accroding to clicking on backword arrow.
*/
private void moveBackWard() {
int currentPosition = viewPager.getCurrentItem();
if (currentPosition > 0) {
int nextPosition = currentPosition - 1;
viewPager.setCurrentItem(nextPosition);
mCollapsingToolbar.setTitle(getCollapsingToolbarTitle(taskAddedMonths.get(nextPosition)));
}
}
/**
* move view pager page as according to clicking on forward arrow.
*/
private void moveForward() {
int currentPosition = viewPager.getCurrentItem();
if (currentPosition < viewPagerSize-1) {
int nextPosition = currentPosition + 1;
if (viewPagerSize == 1) {
//do nothing
} else {
viewPager.setCurrentItem(currentPosition + 1);
mCollapsingToolbar.setTitle(getCollapsingToolbarTitle(taskAddedMonths.get(nextPosition)));
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
taskAddedMonths = DatabaseManager.getInstance(HistoryActivity.this).getAllMonths();
viewPagerSize = taskAddedMonths.size();
mCollapsingToolbar.setTitle(getCollapsingToolbarTitle(taskAddedMonths.get(0)));
mCollapsingToolbar.setExpandedTitleColor(Color.WHITE);
toolbar.setNavigationIcon(ContextCompat.getDrawable(this, R.drawable.ic_backarrow));
viewPager.setAdapter(new ViewPagerAdapter(HistoryActivity.this, DatabaseManager.getInstance(HistoryActivity.this).getAllMonths()));
viewPager.addOnPageChangeListener(this);
}
@Override
public int getLayoutId() {
return R.layout.activity_history;
}
@Override
public Toolbar getToolbar() {
return toolbar;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_history, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
/**
* get the name of month with concatenating History as like this{May History};
*
* @param month
* @return
*/
private String getCollapsingToolbarTitle(String month) {
return month + " " + "History";
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
/******************* what i have done*******************/
//change the title of collapsing toolbar accroding to change in page of viewpager
mCollapsingToolbar.setTitle(getCollapsingToolbarTitle(taskAddedMonths.get(position)));
}
@Override
public void onPageScrollStateChanged(int state) {
}
}