Динамически изменять подсказку редактирования текста на панели инструментов на вкладках смахивания

Вот макет панели инструментов

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@color/ColorPrimary"
android:elevation="2dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
xmlns:android="http://schemas.android.com/apk/res/android" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- Editext for Search -->
    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:drawableLeft="@drawable/ic_search_white"
        android:inputType="textVisiblePassword"/>


</LinearLayout>


</android.support.v7.widget.Toolbar>

и вот основной макет

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    />

<com.example.ic071034.materialstabs.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="2dp"
    android:background="@color/ColorPrimary"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="1">
</android.support.v4.view.ViewPager>


</LinearLayout>

и вот фрагмент вкладки (смахивание)

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class Tab1 extends Fragment {

private ListView mListView;
private ListviewContactAdapter adapter;
private ArrayList<ListviewContactItem> listContact;
private TextView searchbox;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab1, container, false);


    mListView = (ListView) rootView.findViewById(R.id.list);
    listContact = GetlistContact();
    adapter=new ListviewContactAdapter(getActivity(), listContact);
    mListView.setAdapter(adapter);
    return rootView;
}
}

Теперь я хочу установить текст подсказки в тексте редактирования на панели инструментов, когда я смахиваю вкладку, например, когда я смахиваю на tab1, я хочу подсказку как "xyz", а когда я смахиваю на tab2, она должна отображать "wxy"

основной код компоновки

package com.example.ic071034.materialstabs;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;   
import android.support.v7.widget.Toolbar;


public class MainActivity extends ActionBarActivity {

// Declaring Your View and Variables

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Locations","Lines"};
String[] Location ={"xyz","xyz","xyzBoard","xyzLayout","xyz","xyz","xyzxyz","xyz"};
String[] Lines ={"S1","S2","S3","S4","S5","S6","S7","S8"};
int Numboftabs =2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Creating The Toolbar and setting it as the Toolbar for the activity

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);


    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);



    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true,     This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);



}


 /*   @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_main, 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 == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
*/
}

1 ответ

Сначала начните свой TextView в MainActivity в onCreate()

final TextView inputSearch = (TextView) findViewById(R.id.inputSearch);

после этого для ViewPager добавить слушателя

pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    String tabName = //get tab name by tab position
                    inputSearch.setHint(tabName);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
Другие вопросы по тегам