android 仿考题左右滑动切换到下一题
发布网友
发布时间:2022-04-23 20:59
我来回答
共2个回答
热心网友
时间:2023-07-31 15:04
用ViewPager + Fragment,很简单的,
package com.example.viewpagerdemo;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
private ViewPager mPageVp;
private List<Fragment> mFragmentList = new ArrayList<Fragment>();
private FragmentAdapter mFragmentAdapter;
/**
* Tab显示内容TextView
*/
private TextView mTabChatTv, mTabContactsTv, mTabFriendTv;
/**
* Fragment
*/
private ChatFragment mChatFg;
private FriendFragment mFriendFg;
private ContactsFragment mContactsFg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findById();
init();
}
private void findById() {
mTabContactsTv = (TextView) this.findViewById(R.id.id_contacts_tv);
mTabChatTv = (TextView) this.findViewById(R.id.id_chat_tv);
mTabFriendTv = (TextView) this.findViewById(R.id.id_friend_tv);
mPageVp = (ViewPager) this.findViewById(R.id.id_page_vp);
}
private void init() {
mFriendFg = new FriendFragment();
mContactsFg = new ContactsFragment();
mChatFg = new ChatFragment();
//Bundle bundle = new Bundle();
//bundle.putSerializable("subjecBean", subjectBean);
// frgmt.setArguments(bundle);
mFragmentList.add(mChatFg);
mFragmentList.add(mFriendFg);
mFragmentList.add(mContactsFg);
mFragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(), mFragmentList);
mPageVp.setAdapter(mFragmentAdapter);
mPageVp.setCurrentItem(0);
mPageVp.setOnPageChangeListener(pageChangeListener);
}
private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
/**
* state滑动中的状态 有三种状态(0,1,2) 1:正在滑动 2:滑动完毕 0:什么都没做。
*/
@Override
public void onPageScrollStateChanged(int state) {
}
/**
* position :当前页面,及你点击滑动的页面 offset:当前页面偏移的百分比 offsetPixels:当前页面偏移的像素位置
*/
@Override
public void onPageScrolled(int position, float offset, int offsetPixels) {
}
@Override
public void onPageSelected(int position) {
resetTextView();
switch (position) {
case 0:
mTabChatTv.setTextColor(Color.BLUE);
break;
case 1:
mTabFriendTv.setTextColor(Color.BLUE);
break;
case 2:
mTabContactsTv.setTextColor(Color.BLUE);
break;
}
}
};
/**
* 重置颜色
*/
private void resetTextView() {
mTabChatTv.setTextColor(Color.BLACK);
mTabFriendTv.setTextColor(Color.BLACK);
mTabContactsTv.setTextColor(Color.BLACK);
}
}
package com.example.viewpagerdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FriendFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View friendView = inflater.inflate(R.layout.activity_tab_friend, container, false);
return friendView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
package com.example.viewpagerdemo;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class FragmentAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList = new ArrayList<Fragment>();
public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
追问请给个代码吧,给个例子
追答大概就那样的
热心网友
时间:2023-07-31 15:05
我表示没看懂哈哈