1、主界面布局:activity_four_three.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/ll_iv_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="45dp">
LinearLayout>
LinearLayout>
2、主界面Activity:FourThreeActivity
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.fpp.status.R;
import com.fpp.status.activity.fragmenteight.fragment.AllFragment;
import com.fpp.status.activity.fragmenteight.fragment.AlreadyAccomplishFragment;
import com.fpp.status.activity.fragmenteight.fragment.AlreadyCancelFragment;
import com.fpp.status.activity.fragmenteight.fragment.UnderwayFragment;
import com.fpp.status.activity.fragmenteight.fragment.WaitAcceptFragment;
import com.fpp.status.entity.LoadMemberListResponseData;
import com.fpp.status.utils.LogUtils;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by fupengpeng on 2017/12/16 0016.
*/
public class FourThreeActivity extends AppCompatActivity {
@BindView(R.id.viewpager)
ViewPager viewpager;
@BindView(R.id.ll_iv_content)
LinearLayout llIvContent;
//viewpager页面
List viewPagerContentList = new ArrayList();
//viewpager提示
List viewPagerTitleList = new ArrayList();
//viewpager数据
List loadMLRDataList = new ArrayList();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_four_three);
ButterKnife.bind(this);
initViewPager();
}
/**
* 初始化view
*/
private void initViewPager() {
getData();
for (int i = 0; i < loadMLRDataList.size(); i++) {
//获取viewpager界面所要添加的布局view
View vpView = View.inflate(this, R.layout.view_pager_item_four_three, null);
//获取viewpager界面所要添加的布局view中的textview并设置数据
TextView tv = (TextView) vpView.findViewById(R.id.tv_atvt_billing_select_member_name);
tv.setText(loadMLRDataList.get(i).getTruename());
//添加到viewpager的数据中
viewPagerContentList.add(vpView);
//获取提示的布局
View vpViewIv = View.inflate(this, R.layout.view_pager_item_four_three_iv, null);
viewPagerTitleList.add(vpViewIv);
}
// 圆点view的添加
initLinearLayout();
final ViewPagerViewAdapter viewPagerViewAdapter = new ViewPagerViewAdapter(viewPagerContentList);
viewpager.setAdapter(viewPagerViewAdapter);
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(final int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(final int position) {
if (position == viewPagerTitleList.size() - 1) {
LogUtils.e("onPageSelected----position = " + position + " 等于size-1");
viewPagerTitleList.get(position - 1).findViewById(R.id.ll_iv).setBackgroundResource(R.drawable.yuan_jiao_select_iv_bai);
} else if (position == 0) {
LogUtils.e("onPageSelected----position = " + position + " 等于0+1");
viewPagerTitleList.get(position + 1).findViewById(R.id.ll_iv).setBackgroundResource(R.drawable.yuan_jiao_select_iv_bai);
} else {
LogUtils.e("onPageSelected----position = " + position + " 不等于size");
viewPagerTitleList.get(position - 1).findViewById(R.id.ll_iv).setBackgroundResource(R.drawable.yuan_jiao_select_iv_bai);
viewPagerTitleList.get(position + 1).findViewById(R.id.ll_iv).setBackgroundResource(R.drawable.yuan_jiao_select_iv_bai);
}
viewPagerTitleList.get(position).findViewById(R.id.ll_iv).setBackgroundResource(R.drawable.yuan_jiao_select_iv_hui);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
/**
* 获取数据
*/
private void getData() {
for (int i = 0; i < 5; i++) {
LoadMemberListResponseData loadMLRData = new LoadMemberListResponseData();
loadMLRData.setShopname("店铺名" + i);
loadMLRData.setTruename("昵称" + i);
loadMLRData.setLevel(i + "");
loadMLRData.setLevelname("星级" + i);
loadMLRData.setFaceurl("头像" + i);
loadMLRDataList.add(loadMLRData);
}
}
/**
* 提示view添加
*/
private void initLinearLayout() {
for (int i = 0; i < viewPagerTitleList.size(); i++) {
if (i == 0) {
viewPagerTitleList.get(i).findViewById(R.id.ll_iv).setBackgroundResource(R.drawable.yuan_jiao_select_iv_hui);
llIvContent.addView(viewPagerTitleList.get(i));
} else {
viewPagerTitleList.get(i).findViewById(R.id.ll_iv).setBackgroundResource(R.drawable.yuan_jiao_select_iv_bai);
llIvContent.addView(viewPagerTitleList.get(i));
}
}
}
}
3、ViewPager适配器:ViewPagerViewAdapter
/**
* viewpager适配器
*/
public class ViewPagerViewAdapter extends PagerAdapter {
List viewPagerContentList;
public ViewPagerViewAdapter(List viewPagerContentList) {
this.viewPagerContentList = viewPagerContentList;
}
@Override
public int getCount() {
if (viewPagerContentList != null && viewPagerContentList.size() > 0) {
return viewPagerContentList.size();
} else {
return 0;
}
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewPagerContentList.get(position));
return viewPagerContentList.get(position);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
4、动态添加布局:view_pager_item_four_three.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_weight="0.81"
android:background="#ffffff">
<TextView
android:id="@+id/tv_atvt_billing_select_shop_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="白宫"
android:textSize="@dimen/order_subheading_text_size"
android:visibility="gone" />
<ImageView
android:id="@+id/iv_atvt_billing_select_member_pic"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="@drawable/shilipic"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_above="@+id/ll_atvt_billing_select_member_message"
android:layout_alignParentStart="true"
android:id="@+id/relativeLayout">
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="#f5f5f5" />
<LinearLayout
android:id="@+id/ll_atvt_billing_select_member_level"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:visibility="gone">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center"
android:background="#ff0000" />
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center"
android:background="#ff00ff" />
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center"
android:background="#ffff00" />
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center"
android:background="#ffffff" />
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center"
android:background="#00ffff" />
LinearLayout>
RelativeLayout>
<RelativeLayout
android:id="@+id/ll_atvt_billing_select_member_message"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_atvt_billing_select_member_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3.75dp"
android:gravity="center"
android:text="刘涛"
android:textSize="@dimen/order_title_text_size" />
<TextView
android:id="@+id/tv_atvt_billing_select_member_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tv_atvt_billing_select_member_name"
android:layout_alignBottom="@+id/tv_atvt_billing_select_member_name"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@+id/tv_atvt_billing_select_member_name"
android:text="金牌会员"
android:textColor="@color/leftMenuWorkNameTextColor"
android:textSize="@dimen/order_main_body_text_size"
android:visibility="gone" />
RelativeLayout>
RelativeLayout>
LinearLayout>
5、 圆点提示布局:view_pager_item_four_three_iv.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ll_iv"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#ff0000" />
LinearLayout>
6、 圆点提示样式设置:yuan_jiao_select_iv_bai.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />
<stroke
android:width="2dp"
android:color="#c8c8c8"
/>
<corners
android:radius="7.5dp"
/>
shape>