安卓 翻页效果 (特效)

MainActivity

public class MainActivity extends AppCompatActivity implements Callback, FlipView.OnFlipListener, FlipView.OnOverFlipListener {
    FlipView flipView;
    MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        flipView = findViewById(R.id.flip_view);
        myAdapter = new MyAdapter(this);
        myAdapter.setCallback(new MyAdapter.Callback() {
            @Override
            public void onPageRequested(int page) {
                flipView.smoothFlipTo(page);

            }
        });
        flipView.setAdapter(myAdapter);
        flipView.setOnFlipListener(this);
        flipView.peakNext(false);
        flipView.setOverFlipMode(OverFlipMode.RUBBER_BAND);
        flipView.setEmptyView(findViewById(R.id.empty_view));
        flipView.setOnOverFlipListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    public void onPageRequested(int page) {
        flipView.smoothFlipTo(page);
    }

    @Override
    public void onFlippedToPage(FlipView v, int position, long id) {
        Log.i("pageflip", "Page: " + position);
        if (position > flipView.getPageCount() - 3 && flipView.getPageCount() < 30) {
            myAdapter.addItems(5);
        }
    }

    @Override
    public void onOverFlip(FlipView v, OverFlipMode mode, boolean overFlippingPrevious, float overFlipDistance, float flipDistancePerPage) {
        Log.i("overflip", "overFlipDistance = " + overFlipDistance);
    }
}

activity_main.xml



    
   
        tools:context=".MainActivity" >
    

    
```
   

MyAdapter

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends BaseAdapter implements View.OnClickListener {
    public interface Callback {
        public void onPageRequested(int page);
    }

    static class Item {
        static long id = 0;

        long mId;

        public Item() {
            mId = id++;
        }

        long getId() {
            return mId;
        }
    }

    private LayoutInflater inflater;
    private Callback callback;
    private List items = new ArrayList();

    public MyAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        for (int i = 0; i < 10; i++) {
            items.add(new Item());
        }
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

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

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

    @Override
    public long getItemId(int position) {
        return items.get(position).getId();
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.page, parent, false);

            holder.text = (TextView) convertView.findViewById(R.id.text);
            holder.firstPage = (Button) convertView.findViewById(R.id.first_page);
            holder.lastPage = (Button) convertView.findViewById(R.id.last_page);

            holder.firstPage.setOnClickListener(this);
            holder.lastPage.setOnClickListener(this);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //TODO set a text with the id as well
        holder.text.setText(items.get(position).getId() + ":" + position);

        return convertView;
    }

    static class ViewHolder {
        TextView text;
        Button firstPage;
        Button lastPage;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.first_page:
                if (callback != null) {
                    callback.onPageRequested(0);
                }
                break;
            case R.id.last_page:
                if (callback != null) {
                    callback.onPageRequested(getCount() - 1);
                }
                break;
        }
    }

    public void addItems(int amount) {
        for (int i = 0; i < amount; i++) {
            items.add(new Item());
        }
        notifyDataSetChanged();
    }

    public void addItemsBefore(int amount) {
        for (int i = 0; i < amount; i++) {
            items.add(0, new Item());
        }
        notifyDataSetChanged();
    }
}



page.xml




    

strings.xml

    First
    Last
    Prepend

你可能感兴趣的:(安卓 翻页效果 (特效))