gpt4 book ai didi

java - 如何在使用新的android滑动选项卡技术时处理listview事件?

转载 作者:行者123 更新时间:2023-12-01 11:25:33 26 4
gpt4 key购买 nike

我尝试搜索互联网,但发现所有教程都使用旧的 tabhost 技术。我正在使用新的工具栏并在其下添加选项卡。我想在特定选项卡聚焦时在 ListView 中动态显示从数据库获取的内容。以下是我的 Activity 代码:[编辑]

 public class PostStory extends ActionBarActivity implements ViewPager.OnPageChangeListener   {



// Declaring Your View and Variables



Toolbar toolbar;

ViewPager pager;

ViewPagerAdapter adapter;

SlidingTabLayout tabs;

CharSequence Titles[]={"Explore","Publish","Profile"};

int Numboftabs =3;



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_post_story);



// 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);

pager.setOnPageChangeListener(this);

// 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);


Log.d("tabsid ",Integer.toString(tabs.getId()));




}





@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);

}
@Override
public void onPageSelected(int position) {
// if(position == ?) fetchAndSetData(); // Here fetch and set the data
Log.d("pos ",Integer.toString(position));
Toast.makeText(this,"page changed",Toast.LENGTH_LONG);
}

@Override
public void onPageScrollStateChanged(int state) {}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}



}

这是我的选项卡 Activity xml 代码

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context="aru.cramine.studio.aru.PostStory">

<item android:id="@+id/action_publish" android:title="@string/Publish"
android:orderInCategory="1" app:showAsAction="always" />
<item android:id="@+id/action_save" android:title="@string/Save"
android:orderInCategory="2" app:showAsAction="always"

/>


<item android:id="@+id/action_cancel" android:title="@string/Cancel"
android:orderInCategory="3" app:showAsAction="ifRoom" />
</menu>

这是我的 tab.java 代码公共(public)类 Tab1 扩展 Fragment {

    @Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View v =inflater.inflate(R.layout.tab_1,container,false);



return v;

}

}

和tab.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

</ListView>

</RelativeLayout>

最佳答案

您可以添加一个 OnPageChangeListener,如果选择了特定选项卡,您将获取数据并用它填充 ListVIew

public class PostStory extends ActionBarActivity implements ViewPager.OnPageChangeListener {
...

@Override
protected void onCreate(Bundle savedInstanceState) {
...
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
pager.setOnPageChangeListener(this);
...
}

@Override
public void onPageSelected(int position) {
if(position == ?) fetchAndSetData(); // Here fetch and set the data
}

@Override
public void onPageScrollStateChanged(int state) {}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

...
}

关于java - 如何在使用新的android滑动选项卡技术时处理listview事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30833767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com