gpt4 book ai didi

带有 View 寻呼机的Android滑动 View

转载 作者:行者123 更新时间:2023-11-29 21:08:06 24 4
gpt4 key购买 nike

基于android创建Swipe Views with Tabs

http://developer.android.com/training/implementing-navigation/lateral.html

如何使用带有自定义适配器的 ListView 并在此示例中使用?我的意思是显示简单的 textview,显示为。

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >

<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />

<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />

</RelativeLayout>

结果应与 Play 商店应用程序相同。

谢谢。

最佳答案

你可以试试看我的其他帖子:

Android List view layout Similar to Google play

Unable to use AndroidDrawer (sidebar like facebook)

也许这就是您正在寻找的:

How to use swipe gesture in a view pager's page with android?

试试这些链接..也许这就是您要找的。

这是一个更新

1) 制作一个适配器来处理您的 fragment :

public class MyAdapter extends FragmentStatePagerAdapter {

public MyAdapter(FragmentManager fm) {

super(fm);

}

@Override
public Fragment getItem(int index) {

switch (index) {
case 0:

return new FirstFragment();

case 1:

return new SecondFragment();
case 2:

return new ThirdFragment();

}
return null;

}

@Override
public int getCount() {
return 3;
}

}

2) 添加 fragment :我只是为一个 fragment 添加代码。你可以按照那个。

public class FirstFragment extends Fragment {

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

View view = inflater.inflate(R.layout.somefragment, container, false);//You will have your custom layout and add more items.

return view;

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);

TextView tv = (TextView)getActivity().findViewById(R.id.textview1);
//You can add different items in here according to your needs.
//You will have your own code to do stuff. Like to use a listview or a gridview etc. You will make a separate adapter in a separate class and use that adapter in this onActivityCreated() method.
}


}

3)修改MainActivity:

public class AllActivities extends FragmentActivity implements ActionBar.TabListener {

public ViewPager viewPager;
private MyAdapter mAdapter;
private ActionBar actionBar;
private String [] tabs = {"FirstFragment","SecondFragment","ThirdFragment"};

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

//Initializing all stuff
viewPager = (ViewPager)findViewById(R.id.pager);

actionBar = getActionBar();
mAdapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);



//Add the tabs here
for(String tab_name:tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}

viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){

@Override
public void onPageSelected(int position){

//on Page change, that particular page should be selected
actionBar.setSelectedNavigationItem(position);
}

@Override
public void onPageScrolled(int arg0,float arg1,int arg2){

}
@Override
public void onPageScrollStateChanged(int position){

}

});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {

viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {



}

@Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {

viewPager.setCurrentItem(tab.getPosition());

}
}

在你实现了这个例子之后,你就不用担心这个MainActivity了。您只需添加更多 fragment 即可将 fragment 添加到您的适配器并根据它构建页面。另外,添加 private String [] tabs = {"FirstFragment","SecondFragment","ThirdFragment","你定义其他 fragment "};

要使 google play 像抽屉导航一样,您可以引用我的前两个链接,然后根据需要进行操作。

希望这对您正在寻找的东西有所帮助。

关于带有 View 寻呼机的Android滑动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859235/

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