Listview嵌套Viewpager实现仿淘宝搜狐广告主页,并实现listview的下拉刷新

Android实现功能:Listview嵌套viewpager仿淘宝搜狐视频主页面,和listview的下拉刷新。

什么都不说了:直接上图说效果

listview嵌套viewpager实现仿淘宝的广告滑动主页面

源码连接:(http://download.csdn.net/detail/qq_30000411/9528977)

APK下载连接:(http://download.csdn.net/detail/qq_30000411/9528973)

下面给出我源码的主要文件构成:

MyListView文件:

主要功能重写listview实现下拉刷新

package com.example.com.xiyouliwp.demo;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.Date;

/**
 * Created by lwp940118 on 2016/5/13.
 * 需求:重写listview实现下拉刷新
 * 李文朋
 */
public class MyListView extends ListView implements AbsListView.OnScrollListener {

    private boolean isRecored;

    private final static int RELEASE_To_REFRESH = 0;
    private final static int PULL_To_REFRESH = 1;
    private final static int REFRESHING = 2;
    private final static int DONE = 3;
    private final static int LOADING = 4;

    // 实际的padding的距离与界面上偏移距离的比例
    private final static int RATIO = 3;

    private OnRefreshListener refreshListener;

    //下拉刷新的控件定义
    private TextView textView_recommend_old;
    private TextView textView_recommend_new;
    private ImageView imageView_recommend_refresh;
    private ProgressBar progressBar_recommend_refresh;
    private int startY;
    private int firstItemIndex;

    private LayoutInflater inflater;
    private LinearLayout layout;

    //定义下拉刷新显示的东西的宽高
    private int headContentWidth;
    private int headContentHeight;

    private RotateAnimation animation;
    private RotateAnimation reverseAnimation;
    private int state;

    private boolean isRefreshable;
    private boolean isBack;

    public MyListView(Context context) {
        super(context);
        inti(context);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inti(context);
    }

    private void inti(Context context) {

        setCacheColorHint(context.getResources().getColor(R.color.transparent));
        inflater = LayoutInflater.from(context);
        layout = (LinearLayout) inflater.inflate(R.layout.mylistview_shuaxin, null);
        textView_recommend_new = (TextView) layout.findViewById(R.id.textview_recommend_refresh_new);
        textView_recommend_old = (TextView) layout.findViewById(R.id.textview_recommend_refresh_old);
        imageView_recommend_refresh = (ImageView) layout.findViewById(R.id.imgeview_recommend_refresh);
        progressBar_recommend_refresh = (ProgressBar) layout.findViewById(R.id.progressbar_recommend_refrsh);

        //设置下拉刷新图标的宽高
        imageView_recommend_refresh.setMinimumHeight(50);
        imageView_recommend_refresh.setMinimumWidth(70);
        measureView(layout);

        headContentHeight = layout.getMeasuredHeight();
        headContentWidth = layout.getMeasuredWidth();

        layout.setPadding(0, -1 * headContentHeight, 0, 0);
        layout.invalidate();

        Log.v("size", "width:" + headContentWidth + " height:"
                + headContentHeight);

        addHeaderView(layout, null, false);
        setOnScrollListener(this);

        animation = new RotateAnimation(0, -180,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        animation.setInterpolator(new LinearInterpolator());
        animation.setDuration(250);
        animation.setFillAfter(true);

        reverseAnimation = new RotateAnimation(-180, 0,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        reverseAnimation.setInterpolator(new LinearInterpolator());
        reverseAnimation.setDuration(200);
        reverseAnimation.setFillAfter(true);

        state = DONE;

        isRefreshable = false;

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isRefreshable) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (firstItemIndex == 0 && !isRecored) {
                        isRecored = true;
                        startY = (int) event.getY();
                    }
                    break;

                case MotionEvent.ACTION_UP:

                    if (state != REFRESHING && state != LOADING) {
                        if (state == DONE) {
                            // 什么都不做
                        }
                        if (state == PULL_To_REFRESH) {
                            state = DONE;
                            changeHeaderViewByState();

                        }
                        if (state == RELEASE_To_REFRESH) {
                            state = REFRESHING;
                            changeHeaderViewByState();
                            onRefresh();

                        }
                    }

                    isRecored = false;
                    isBack = false;

                    break;

                case MotionEvent.ACTION_MOVE:
                    int tempY = (int) event.getY();

                    if (!isRecored && firstItemIndex == 0) {
                        isRecored = true;
                        startY = tempY;
                    }

                    if (state != REFRESHING && isRecored && state != LOADING) {

                        // 保证在设置padding的过程中,当前的位置一直是在head,否则如果当列表超出屏幕的话,当在上推的时候,列表会同时进行滚动

                        // 可以松手去刷新了
                        if (state == RELEASE_To_REFRESH) {

                            setSelection(0);

                            // 往上推了,推到了屏幕足够掩盖head的程度,但是还没有推到全部掩盖的地步
                            if (((tempY - startY) / RATIO < headContentHeight)
                                    && (tempY - startY) > 0) {
                                state = PULL_To_REFRESH;
                                changeHeaderViewByState();
                            }
                            // 一下子推到顶了
                            else if (tempY - startY <= 0) {
                                state = DONE;
                                changeHeaderViewByState();

                            }
                            // 往下拉了,或者还没有上推到屏幕顶部掩盖head的地步
                            else {
                                // 不用进行特别的操作,只用更新paddingTop的值就行了
                            }
                        }
                        // 还没有到达显示松开刷新的时候,DONE或者是PULL_To_REFRESH状态
                        if (state == PULL_To_REFRESH) {

                            setSelection(0);

                            // 下拉到可以进入RELEASE_TO_REFRESH的状态
                            if ((tempY - startY) / RATIO >= headContentHeight) {
                                state = RELEASE_To_REFRESH;
                                isBack = true;
                                changeHeaderViewByState();

                            }
                            // 上推到顶了
                            else if (tempY - startY <= 0) {
                                state = DONE;
                                changeHeaderViewByState();

                            }
                        }

                        // done状态下
                        if (state == DONE) {
                            if (tempY - startY > 0) {
                                state = PULL_To_REFRESH;
                                changeHeaderViewByState();
                            }
                        }

                        // 更新headView的size
                        if (state == PULL_To_REFRESH) {
                            layout.setPadding(0, -1 * headContentHeight
                                    + (tempY - startY) / RATIO, 0, 0);

                        }

                        // 更新headView的paddingTop
                        if (state == RELEASE_To_REFRESH) {
                            layout.setPadding(0, (tempY - startY) / RATIO
                                    - headContentHeight, 0, 0);
                        }

                    }

                    break;
            }

        }
        return super.onTouchEvent(event);
    }

    private void onRefresh() {
        if (refreshListener != null) {
            refreshListener.onRefresh();
        }
    }

    public interface OnRefreshListener {
        public void onRefresh();
    }

    private void changeHeaderViewByState() {

        switch (state) {
            case RELEASE_To_REFRESH:
                imageView_recommend_refresh.setVisibility(View.VISIBLE);
                progressBar_recommend_refresh.setVisibility(View.GONE);
                textView_recommend_new.setVisibility(View.VISIBLE);
                textView_recommend_old.setVisibility(View.VISIBLE);

                imageView_recommend_refresh.clearAnimation();
                imageView_recommend_refresh.startAnimation(animation);

                textView_recommend_new.setText("松开刷新");

                break;
            case PULL_To_REFRESH:
                progressBar_recommend_refresh.setVisibility(View.GONE);
                textView_recommend_new.setVisibility(View.VISIBLE);
                textView_recommend_old.setVisibility(View.VISIBLE);
                imageView_recommend_refresh.clearAnimation();
                imageView_recommend_refresh.setVisibility(View.VISIBLE);
                // 是由RELEASE_To_REFRESH状态转变来的
                if (isBack) {
                    isBack = false;
                    imageView_recommend_refresh.clearAnimation();
                    imageView_recommend_refresh.startAnimation(reverseAnimation);

                    textView_recommend_new.setText("下拉刷新");
                } else {
                    textView_recommend_new.setText("下拉刷新");
                }
                break;

            case REFRESHING:

                layout.setPadding(0, 0, 0, 0);

                progressBar_recommend_refresh.setVisibility(View.VISIBLE);
                imageView_recommend_refresh.clearAnimation();
                imageView_recommend_refresh.setVisibility(View.GONE);
                textView_recommend_new.setText("正在刷新...");
                textView_recommend_old.setVisibility(View.VISIBLE);

                break;
            case DONE:
                layout.setPadding(0, -1 * headContentHeight, 0, 0);

                progressBar_recommend_refresh.setVisibility(View.GONE);
                imageView_recommend_refresh.clearAnimation();
                imageView_recommend_refresh.setImageResource(R.drawable.arrow);
                textView_recommend_new.setText("下拉刷新");
                textView_recommend_old.setVisibility(View.VISIBLE);

                break;
        }

    }

    public void setonRefreshListener(OnRefreshListener refreshListener) {
        this.refreshListener = refreshListener;
        isRefreshable = true;
    }
    public void onRefreshComplete() {
        state = DONE;
        textView_recommend_old.setText("最近更新:" + new Date().toLocaleString());
        changeHeaderViewByState();
    }

    public void setAdapter(BaseAdapter adapter) {
        textView_recommend_old.setText("最近更新:" + new Date().toLocaleString());
        super.setAdapter(adapter);
    }

    //计算估计下拉刷新的所需要的宽高

    private void measureView(View view) {

        ViewGroup.LayoutParams p = view.getLayoutParams();
        if (p == null) {
            p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        int viewWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
        int lpHeight = p.height;
        int viewHeightSpec;
        if (lpHeight > 0) {
            viewHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
                    MeasureSpec.EXACTLY);
        } else {
            viewHeightSpec = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
        }
        view.measure(viewWidthSpec, viewHeightSpec);

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        firstItemIndex = firstVisibleItem;

    }
}

MyViewPager文件:

主要功能,重写viewpager,为了避免listview和viewpager的焦点冲突。

package com.example.com.xiyouliwp.demo;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;

/**
 * Created by lwp940118 on 2016/5/12.
 * 李文朋
 */
public class MyViewPager extends ViewPager {

    private ViewGroup parent;

    public MyViewPager(Context context) {
        super(context);
    }

    public MyViewPager(Context context, AttributeSet attributeSet) {
        super(context,attributeSet);
    }

    public void setNestedpParent(ViewGroup parent) {
        this.parent = parent;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
        }
        return super.onInterceptTouchEvent(arg0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
        }
        return super.onTouchEvent(arg0);
    }

}

MainActivity文件:

package com.example.com.xiyouliwp.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.com.xiyouliwp.demo.R;

public class MainActivity extends Activity {

    private MyViewPager viewpager;
    private List<ImageView> imageViews; // 滑动图片的数组
    private String[] titleStrings; // 滑动图片的标题
    private int[] imagevierid; // 滑动图片的id;
    private List<View> views; // 滑动原点的view
    private int currentItem = 0; // 当前图片的索引号
    private ScheduledExecutorService scheduledExecutorService;

    private TextView textView_recommend_yuan;

    private MyListView listView_recommend;
    private ListView_Adapter list_adapter;
    // 切换当前图片
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // 显示当前图片
            viewpager.setCurrentItem(currentItem);
        }
    };

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

        initViewpager();
        initListview();
    }

    private void initViewpager() {
        View header = LayoutInflater.from(MainActivity.this).inflate(
                R.layout.myviewpager, null);
        viewpager = (MyViewPager) header.
                findViewById(R.id.viewpage_recommend);

        imagevierid = new int[]{R.drawable.view1, R.drawable.view2, R.drawable.view3, R.drawable.viewtu
                , R.drawable.view5};
        titleStrings = new String[imagevierid.length];

        titleStrings[0] = "宠物的再生圣地";
        titleStrings[1] = "商店交易";
        titleStrings[2] = "狗狗专属衣物";
        titleStrings[3] = "萌宠秀翻天";
        titleStrings[4] = "宠物人性化培训";

        imageViews = new ArrayList<ImageView>();
        //初始化图片资源
        for (int i = 0; i < imagevierid.length; i++) {
            ImageView imageView = new ImageView(MainActivity.this);
            imageView.setBackgroundResource(imagevierid[i]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageViews.add(imageView);
        }

        textView_recommend_yuan = (TextView) header.findViewById(R.id.textview_Viewpager);

        //初始化原点
        views = new ArrayList<View>();
        views.add(header.findViewById(R.id.view_recommend_yuan1));
        views.add(header.findViewById(R.id.view_recommend_yuan2));
        views.add(header.findViewById(R.id.view_recommend_yuan3));
        views.add(header.findViewById(R.id.view_recommend_yuan4));
        views.add(header.findViewById(R.id.view_recommend_yuan5));
        textView_recommend_yuan.setText(titleStrings[0]);

        viewpager.setNestedpParent((ViewGroup) viewpager.getParent());

        //Viewpager的adapter
        // viewPager_recommend.setAdapter(new viewpagerecommendAdapter(getActivity()));

        viewpagerecommendAdapter adapter = new viewpagerecommendAdapter(MainActivity.this);
        viewpager.setAdapter(adapter);
        viewpager.setOnPageChangeListener(new viewpagerRecommendPageChangeListener());

        listView_recommend.addHeaderView(header);

    }

    //viewpagger的PageChangeListener
    private class viewpagerRecommendPageChangeListener implements ViewPager.OnPageChangeListener {

        private int oldposition = 0;

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

        }

        @Override
        public void onPageSelected(int position) {

            currentItem = position;
            textView_recommend_yuan.setText(titleStrings[position]);
            views.get(oldposition).setBackgroundResource(R.drawable.dot_normal);
            views.get(position).setBackgroundResource(R.drawable.dot_focused);
            oldposition = position;

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
     //viewpager的aderpter

    private class viewpagerecommendAdapter extends PagerAdapter {

        private Context mContext;

        public viewpagerecommendAdapter(Context mContext) {
            this.mContext = mContext;

        }

        @Override
        public int getCount() {
            return imagevierid.length;
        }

        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager) container).addView(imageViews.get(position));
            return imageViews.get(position);
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {

        }

        @Override
        public void finishUpdate(View arg0) {

        }

    }

    public class ListView_Adapter extends BaseAdapter {

        private List<String> userInfos; // 标题
        private LayoutInflater layoutInflater = null;

        public ListView_Adapter( List<String> strings) {
            this.userInfos = strings;
            layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public int getCount() {
            return userInfos.size();
        }

        @Override
        public Object getItem(int position) {
            return userInfos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {

                holder = new ViewHolder();

                convertView = layoutInflater.inflate(R.layout.item_mylistview,
                        null);
                holder.textview = (TextView)convertView. findViewById(R.id.textview);

                // 对item中的控件的id进行寻找
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();

                holder.textview.setText(userInfos.get(position));
            }

            return convertView;
        }

        class ViewHolder {
            TextView textview;
        }

    }

    private void initListview() {

        list_adapter = new ListView_Adapter( getArrylst());
        listView_recommend.setAdapter(list_adapter);

        listView_recommend
                .setonRefreshListener(new MyListView.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        new AsyncTask<Void, Void, Void>() {
                            protected Void doInBackground(Void... params) {
                                try {
                                    Thread.sleep(2000);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                return null;
                            }

                            @Override
                            protected void onPostExecute(Void result) {
                                list_adapter.notifyDataSetChanged();
                                listView_recommend.onRefreshComplete();
                            }
                        }.execute();
                    }
                });

    }

    // listview的数据入口

    private List<String> getArrylst() {

        List<String> titleArray = new ArrayList<String>();
        for (int i = 0; i < 50; i++) {
            titleArray.add("dome数据" + i);
        }

        return titleArray;

    }

    @Override
    public void onStart() {
        scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.scheduleAtFixedRate(new YuanDianRun(), 1, 2,
                TimeUnit.SECONDS);
        super.onStart();
    }

    @Override
    public void onStop() {
        scheduledExecutorService.shutdown();
        super.onStop();
    }

    private class YuanDianRun implements Runnable {

        @Override
        public void run() {
            synchronized (viewpager) {
                currentItem = (currentItem + 1) % imageViews.size();
                // handler切换图片
                handler.obtainMessage().sendToTarget();
            }
        }
    }

    // 寻找ID
    private void findid() {

        listView_recommend = (MyListView) findViewById(R.id.listview_recommend);
    }

}

布局文件分别如下:

activity_main文件主要实现主界面的布局

<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" >

    <com.example.com.xiyouliwp.demo.MyListView
        android:id="@+id/listview_recommend"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#ddf0ed"
        android:dividerHeight="5dp" >

    </com.example.com.xiyouliwp.demo.MyListView>

</RelativeLayout>

item_mylistview文件主要实现listview的item的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="demo数据1"
        android:gravity="center"
        android:id="@+id/textview"
        />

</LinearLayout>

mylistview_shuaxin文件主要实现下拉刷新时的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--下拉刷新的内容 -->
    <RelativeLayout
        android:id="@+id/relativeLayout_recommend_refresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="30dp">

        <!-- 下拉刷新的进度条-->
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="#ddf0ed">

            <ImageView
                android:id="@+id/imgeview_recommend_refresh"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/arrow"/>
            <ProgressBar
                android:id="@+id/progressbar_recommend_refrsh"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="?android:attr/progressBarStyleSmall"
                android:visibility="gone"
                android:layout_gravity="center"
                />

        </FrameLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textview_recommend_refresh_new"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="下拉刷新"
                android:textColor="#0e0d0d"
                android:textSize="20dp"/>
            <TextView
                android:id="@+id/textview_recommend_refresh_old"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="最近更新"
                android:textColor="#53199f"
                android:textSize="10dp"
                />

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

myviewpager文件的布局实现广告栏的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="150dp">

        <com.example.com.xiyouliwp.demo.MyViewPager
            android:id="@+id/viewpage_recommend"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </com.example.com.xiyouliwp.demo.MyViewPager>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="35dip"
            android:layout_gravity="bottom"
            android:background="#33000000"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textview_Viewpager"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ipet"
                android:textColor="#ffffff" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dip"
                android:gravity="center">

                <View
                    android:id="@+id/view_recommend_yuan1"
                    style="@style/dot_style"
                    android:background="@drawable/dot_focused" />

                <View
                    android:id="@+id/view_recommend_yuan2"
                    style="@style/dot_style" />

                <View
                    android:id="@+id/view_recommend_yuan3"
                    style="@style/dot_style" />

                <View
                    android:id="@+id/view_recommend_yuan4"
                    style="@style/dot_style" />

                <View
                    android:id="@+id/view_recommend_yuan5"
                    style="@style/dot_style" />
            </LinearLayout>
        </LinearLayout>
    </FrameLayout>

</LinearLayout>

还有drawable文件中的一些XML

dot_focused.xml和dot_normal.xml实现广告滑动上面的五个小圆点

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <solid android:color="#aaFFFFFF" />

    <corners android:radius="5dip" />

</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<solid android:color="#33000000" />

<corners android:radius="5dip" />

</shape>

圆点的style文件如下:

<style name="dot_style">
        <item name="android:layout_width">5dp</item>
        <item name="android:layout_height">5dp</item>
        <item name="android:background">@drawable/dot_normal</item>
        <item name="android:layout_marginLeft">1.5dp</item>
        <item name="android:layout_marginRight">1.5dp</item>
    </style>

还有上面布局文件中的color文件中的颜色代码:

<color name="transparent">#00000000</color>

OK当你看完这些时,你已经成功实现了listview的下拉刷新和listview嵌套viewpager仿淘宝搜狐的广告主页面。

时间: 2024-10-19 19:28:36

Listview嵌套Viewpager实现仿淘宝搜狐广告主页,并实现listview的下拉刷新的相关文章

用Swift实现淘宝和大众点评的下拉刷新

来自Leo的原创博客,转载请著名出处 我的StackOverflow 我的Github https://github.com/LeoMobileDeveloper 效果 淘宝 大众点评 项目地址 其中 大众点评的下拉刷新用了50行左右代码 淘宝的下拉刷新用了90行左右代码 完整的代码地址:PullToRefreshKit Tips:用Swift 2.2写的,所以需要XCode 7.3来运行. PullToRefreshKit 这是我用纯Swift 2.2写的一个库,初衷是为了更简单的实现自定义下

Android学习之——ListView下拉刷新

背景知识 ListView使用非常广泛,对于使用ListView的应用来说,下拉刷新是必不可少要实现的功能. 我们常用的微博.网易新闻,搜狐新闻都使用了这一功能,如下图所示.     微博 搜狐新闻 具体学习: 首先分析下拉刷新的具体操作过程: 用户手指在ListView上按下并往下拉----->出现一个提示的View在ListView顶部----->ListView内容更新,顶部的提示View消失    具体实现步骤:    1.创建继承自ListView的RefreshListView,并

自定义控件学习——下拉刷新ListView

效果 开始用Android Studio写了,还有挺多不明白这IDE用法的地方....蛋疼 主要思路 1. 添加了自定义的头布局    2. 默认让头布局隐藏setPadding.设置 -自身的高度    3. ListView下拉的时候, 修改paddingTop, 让头布局显示出来    4. 触摸动态修改头布局, 根据paddingTop.          - paddingTop = 0 完全显示        - paddingTop < 不完全显示 -64(自身高度)完全隐藏  

android中listView下拉刷新

Android的ListView是应用最广的一个组件,功能强大,扩展性灵活(不局限于ListView本身一个类),前面的文章有介绍分组,拖拽,3D立体,游标,圆角,而今天我们要介绍的是另外一个扩展ListView:下拉刷新的ListView.    下拉刷新界面最初流行于iphone应用界面,如图:     然后在Android中也逐渐被应用,比如微博,资讯类.    所以,今天要实现的结果应该也是类似的,先贴出最终完成效果,如下图,接下来我们一步一步实现. 1. 流程分析    下拉刷新最主要

Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片

Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片 自定义ADPager 自定义水平滚动的ScrollView效仿ViewPager 当遇到要在ViewPager中添加多张网络请求图片的情况下,不能进行复用,导致每次都要重新去求情已经请求过的数据致使流量数据过大 自定义的数据结构解决了这个问题,固定传递的图片数据之后进行统一请求,完成后进行页面切换数据复用 代码中涉及网络请求是用的Volley网络请求框架 PicCarousel是网络数据请求的U

高仿淘宝客户端

高仿淘宝客户端 仿淘宝安卓客户端的demo源码,主要实现了:商品的基本展示.宝贝详情,图片展示的放大缩小功能.界面之间切换的动画.购物车多项删除.弹窗的动画效果.首页广告的轮播效果.获得本机具有传感器的列表.listView的上拉刷新,下拉加载功能.二维码扫描.刮刮乐等功能和效果. 下载地址:http://www.devstore.cn/code/info/925.html 运行截图:    

自定义View之仿淘宝详情页

自定义View之仿淘宝详情页 转载请标明出处: http://blog.csdn.net/lisdye2/article/details/52353071 本文出自:[Alex_MaHao的博客] 项目中的源码已经共享到github,有需要者请移步[Alex_MaHao的github] 基本介绍 现在的一些购物类App例如淘宝,京东等,在物品详情页,都采用了类似分层的模式,即上拉加载详情的方式,节省了空间,使用户的体验更加的舒适.只要对于某个东西的介绍很多时,都可以采取这样的方式,第一个页面显示

高仿淘宝和聚美优品商城详情页实现《IT蓝豹》

android-vertical-slide-view高仿淘宝和聚美优品商城详情页实现,在商品详情页,向上拖动时,可以加载下一页.使用ViewDragHelper,滑动比较流畅. scrollView滑动到底部的时候,再行向上拖动时,添加了一些阻力.本项目来源:https://github.com/xmuSistone/android-vertical-slide-view主要代码如下:首先先看一下布局:  <com.stone.verticalslide.DragLayout        a

Vue实现仿淘宝商品详情属性选择的功能

Vue实现仿淘宝商品详情属性选择的功能 2018年09月07日 17:13:35 yx_cos 阅读数:278 先看下效果图:(同个属性内部单选,属性与属性之间可以多选) 主要实现过程: 所使用到的数据类型是(一个大数组里面嵌套了另一个数组)具体格式如下: attrAndValuees: [   {   'attrId': 1,   'attrName': '味道',   'valuees': [   {   'attrId': 1,   'value': '橘子味'   },   {   'a