Andriod学习教程之滑动布局(14)
本文实例为大家分享了Andriod滑动布局的具体代码,供大家参考,具体内容如下
MainActivity.java代码:
package siso.swipelayoutdemo; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import com.nineoldandroids.view.ViewHelper; import siso.swipelib.SwipeLayout; public class MainActivity extends AppCompatActivity { private SwipeLayout sample1, sample2, sample3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sample1 = (SwipeLayout) findViewById(R.id.sample1); sample1.setShowMode(SwipeLayout.ShowMode.PullOut); View starBottView = sample1.findViewById(R.id.starbott); sample1.addDrag(SwipeLayout.DragEdge.Left, sample1.findViewById(R.id.bottom_wrapper)); sample1.addDrag(SwipeLayout.DragEdge.Right, sample1.findViewById(R.id.bottom_wrapper_2)); sample1.addDrag(SwipeLayout.DragEdge.Top, starBottView); sample1.addDrag(SwipeLayout.DragEdge.Bottom, starBottView); sample1.addRevealListener(R.id.delete, new SwipeLayout.OnRevealListener() { @Override public void onReveal(View child, SwipeLayout.DragEdge edge, float fraction, int distance) { } }); sample1.getSurfaceView().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Click on surface", Toast.LENGTH_SHORT).show(); Log.d(MainActivity.class.getName(), "click on surface"); } }); sample1.getSurfaceView().setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Toast.makeText(MainActivity.this, "longClick on surface", Toast.LENGTH_SHORT).show(); Log.d(MainActivity.class.getName(), "longClick on surface"); return true; } }); sample1.findViewById(R.id.star2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Star", Toast.LENGTH_SHORT).show(); } }); sample1.findViewById(R.id.trash2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Trash Bin", Toast.LENGTH_SHORT).show(); } }); sample1.findViewById(R.id.magnifier2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Magnifier", Toast.LENGTH_SHORT).show(); } }); sample1.addRevealListener(R.id.starbott, new SwipeLayout.OnRevealListener() { @Override public void onReveal(View child, SwipeLayout.DragEdge edge, float fraction, int distance) { View star = child.findViewById(R.id.star); float d = child.getHeight() / 2 - star.getHeight() / 2; ViewHelper.setTranslationY(star, d * fraction); ViewHelper.setScaleX(star, fraction + 0.6f); ViewHelper.setScaleY(star, fraction + 0.6f); } }); //sample2 sample2 = (SwipeLayout) findViewById(R.id.sample2); sample2.setShowMode(SwipeLayout.ShowMode.LayDown); sample2.addDrag(SwipeLayout.DragEdge.Right, sample2.findViewWithTag("Bottom2")); sample2.findViewById(R.id.star).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Star", Toast.LENGTH_SHORT).show(); } }); sample2.findViewById(R.id.trash).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Trash Bin", Toast.LENGTH_SHORT).show(); } }); sample2.findViewById(R.id.magnifier).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Magnifier", Toast.LENGTH_SHORT).show(); } }); sample2.findViewById(R.id.click).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Yo", Toast.LENGTH_SHORT).show(); } }); sample2.getSurfaceView().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Click on surface", Toast.LENGTH_SHORT).show(); } }); //sample3 sample3 = (SwipeLayout) findViewById(R.id.sample3); sample3.addDrag(SwipeLayout.DragEdge.Top, sample3.findViewWithTag("Bottom3")); sample3.addRevealListener(R.id.bottom_wrapper_child1, new SwipeLayout.OnRevealListener() { @Override public void onReveal(View child, SwipeLayout.DragEdge edge, float fraction, int distance) { View star = child.findViewById(R.id.star); float d = child.getHeight() / 2 - star.getHeight() / 2; ViewHelper.setTranslationY(star, d * fraction); ViewHelper.setScaleX(star, fraction + 0.6f); ViewHelper.setScaleY(star, fraction + 0.6f); int c = (Integer) evaluate(fraction, Color.parseColor("#dddddd"), Color.parseColor("#4C535B")); child.setBackgroundColor(c); } }); sample3.findViewById(R.id.bottom_wrapper_child1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Yo!", Toast.LENGTH_SHORT).show(); } }); sample3.getSurfaceView().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Click on surface", Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.my, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_listview) { startActivity(new Intent(this, ListViewExample.class)); return true; } else if (id == R.id.action_gridview) { startActivity(new Intent(this, GridViewExample.class)); return true; } else if (id == R.id.action_nested) { startActivity(new Intent(this, NestedExample.class)); return true; } else if (id == R.id.action_recycler) { startActivity(new Intent(this, RecyclerViewExample.class)); } return super.onOptionsItemSelected(item); } /* Color transition method. */ public Object evaluate(float fraction, Object startValue, Object endValue) { int startInt = (Integer) startValue; int startA = (startInt >> 24) & 0xff; int startR = (startInt >> 16) & 0xff; int startG = (startInt >> 8) & 0xff; int startB = startInt & 0xff; int endInt = (Integer) endValue; int endA = (endInt >> 24) & 0xff; int endR = (endInt >> 16) & 0xff; int endG = (endInt >> 8) & 0xff; int endB = endInt & 0xff; return (int) ((startA + (int) (fraction * (endA - startA))) << 24) | (int) ((startR + (int) (fraction * (endR - startR))) << 16) | (int) ((startG + (int) (fraction * (endG - startG))) << 8) | (int) ((startB + (int) (fraction * (endB - startB)))); } }
NestedExample.java代码:
package siso.swipelayoutdemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Toast; import siso.swipelib.SwipeLayout; public class NestedExample extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.complicate_layout); SwipeLayout swipeLayout = (SwipeLayout)findViewById(R.id.test_swipe_swipe); swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() { @Override public void onDoubleClick(SwipeLayout layout, boolean surface) { Toast.makeText(getApplicationContext(), "DoubleClick", Toast.LENGTH_SHORT).show(); } }); swipeLayout.findViewById(R.id.trash).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "Click", Toast.LENGTH_SHORT).show(); } }); } }
RecyclerViewExample.java代码:
package siso.swipelayoutdemo; import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import java.util.ArrayList; import java.util.Arrays; import jp.wasabeef.recyclerview.animators.FadeInLeftAnimator; import siso.swipelayoutdemo.adapter.RecyclerViewAdapter; import siso.swipelayoutdemo.adapter.util.DividerItemDecoration; import siso.swipelib.util.Attributes; public class RecyclerViewExample extends Activity { private RecyclerView recyclerView; private RecyclerView.Adapter mAdapter; private ArrayList<String> mDataSet; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recyclerview); recyclerView = (RecyclerView) findViewById(R.id.recycler_view); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ActionBar actionBar = getActionBar(); if (actionBar != null) { actionBar.setTitle("RecyclerView"); } } // Layout Managers: recyclerView.setLayoutManager(new LinearLayoutManager(this)); // Item Decorator: recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.divider))); recyclerView.setItemAnimator(new FadeInLeftAnimator()); // Adapter: String[] adapterData = new String[]{"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"}; mDataSet = new ArrayList<String>(Arrays.asList(adapterData)); mAdapter = new RecyclerViewAdapter(this, mDataSet); ((RecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single); recyclerView.setAdapter(mAdapter); /* Listeners */ recyclerView.setOnScrollListener(onScrollListener); } RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); Log.e("ListView", "onScrollStateChanged"); } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); } }; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.my, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_listview) { startActivity(new Intent(this, ListViewExample.class)); finish(); return true; } else if (id == R.id.action_gridview) { startActivity(new Intent(this, GridViewExample.class)); finish(); return true; } return super.onOptionsItemSelected(item); } }
ListViewExample.java代码:
package siso.swipelayoutdemo; import android.app.ActionBar; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import siso.swipelayoutdemo.adapter.ListViewAdapter; import siso.swipelib.SwipeLayout; import siso.swipelib.util.Attributes; public class ListViewExample extends Activity { private ListView mListView; private ListViewAdapter mAdapter; private Context mContext = this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview); mListView = (ListView) findViewById(R.id.listview); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ActionBar actionBar = getActionBar(); if (actionBar != null) { actionBar.setTitle("ListView"); } } mAdapter = new ListViewAdapter(this); mListView.setAdapter(mAdapter); mAdapter.setMode(Attributes.Mode.Single); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ((SwipeLayout)(mListView.getChildAt(position - mListView.getFirstVisiblePosition()))).open(true); } }); mListView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.e("ListView", "OnTouch"); return false; } }); mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(mContext, "OnItemLongClickListener", Toast.LENGTH_SHORT).show(); return true; } }); mListView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { Log.e("ListView", "onScrollStateChanged"); } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }); mListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Log.e("ListView", "onItemSelected:" + position); } @Override public void onNothingSelected(AdapterView<?> parent) { Log.e("ListView", "onNothingSelected:"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.my, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_listview) { startActivity(new Intent(this, ListViewExample.class)); finish(); return true; } else if (id == R.id.action_gridview) { startActivity(new Intent(this, GridViewExample.class)); finish(); return true; } else if (id == R.id.action_recycler) { startActivity(new Intent(this, RecyclerViewExample.class)); finish(); return true; } return super.onOptionsItemSelected(item); } }
GridViewExample.java代码:
package siso.swipelayoutdemo; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import siso.swipelayoutdemo.adapter.GridViewAdapter; import siso.swipelib.util.Attributes; public class GridViewExample extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gridview); final GridView gridView = (GridView)findViewById(R.id.gridview); final GridViewAdapter adapter = new GridViewAdapter(this); adapter.setMode(Attributes.Mode.Multiple); gridView.setAdapter(adapter); gridView.setSelected(false); gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Log.e("onItemLongClick","onItemLongClick:" + position); return false; } }); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.e("onItemClick","onItemClick:" + position); } }); gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Log.e("onItemSelected","onItemSelected:" + position); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } }
app项目结构:
Andriod类库项目结构:
build.gradle
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "siso.swipelayoutdemo" minSdkVersion 22 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.0.1' compile project(path: ':swipelib') compile 'com.android.support:recyclerview-v7:23.3.0' compile 'com.daimajia.easing:library:1.0.0@aar' compile 'com.daimajia.androidanimations:library:1.1.2@aar' compile 'com.nineoldandroids:library:2.4.0' compile 'jp.wasabeef:recyclerview-animators:1.0.3@aar' }
运行结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)