Andriod学习教程之滑动布局(14)_Android

本文实例为大家分享了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'
}

运行结果如图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
, 布局
滑动
,以便于您获取更多的相关知识。

时间: 2024-09-20 20:03:17

Andriod学习教程之滑动布局(14)_Android的相关文章

Android开发笔记之Intent初级学习教程_Android

本文讲述了Android开发笔记之Intent初级学习教程.分享给大家供大家参考,具体如下: 项目创建步骤: New Android Project-> Project name:Intent Build Target:Android 2.2 Application name:IntentDemo Package name:com.b510.intent.activity Create Activity:MainActivity Min SDK Version:8 Finish 1.拨打电话 按

Andriod开发中AsyncHttpClient类库学习教程

AsyncHttpClient client = newAsyncHttpClient(); client.get("http://www.google.com", newAsyncHttpResponseHandler() { @Override public void onSuccess(String response) {   System.out.println(response); } }); 通过AsyncHttpClient类的实例就可以执行网络请求,包括get.put.

值得分享和收藏的Bootstrap学习教程_javascript技巧

首先先感谢朋友们对小编的支持,关注小编的文章,和小编一起学习进步,一直的陪伴,小编也一定会再接再厉,为大家分享更多精彩的文章. 在分享的过程中遇到了一些问题,这也给了我很大的启发,大家在搜索相关文章时都没有针对此类问题进行的系统整理文章,这也是小编需要在今后进行改进的. 所以本文就吸取这个经验,针对Bootstrap教程进行系统整理,希望对大家的学习有所帮助,便于大家学习. 小编的水平能力都很有限,还望大牛多给一些建议和看法,帮助小编进行改正和提高,如果文中确有不妥之处,还请谅解.希望能和大家共

ExtJS 4.2 教程-08:布局系统详解

ExtJS 4.2 系列教程导航目录: ExtJS 4.2 教程-01:Hello ExtJS ExtJS 4.2 教程-02:bootstrap.js 工作方式 ExtJS 4.2 教程-03:使用Ext.define自定义类 ExtJS 4.2 教程-04:数据模型 ExtJS 4.2 教程-05:客户端代理(proxy) ExtJS 4.2 教程-06:服务器代理(proxy) ExtJS 4.2 教程-07:Ext.Direct ExtJS 4.2 教程-08:布局系统详解 今天我们来对

ASP.NET MVC 5 学习教程:创建连接字符串

原文 ASP.NET MVC 5 学习教程:创建连接字符串 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 通过控制器访问模型的数据 生成的代码详解 使用 SQL Server LocalDB Edit方法和Edit视图详解 添加查询 Entity Framework 数据迁移之添加字段 添加验证 Details 和 Delete 方法详解 在上一节中,我们创建了MovieDBContext 类来连接数据

ASP.NET MVC 5 学习教程:添加验证

原文 ASP.NET MVC 5 学习教程:添加验证 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 通过控制器访问模型的数据 生成的代码详解 使用 SQL Server LocalDB Edit方法和Edit视图详解 添加查询 Entity Framework 数据迁移之添加字段 添加验证 Details 和 Delete 方法详解 在本节中,我们将为Movie模型添加验证逻辑,并确认验证规则在用户试图

ASP.NET MVC 5 学习教程:快速入门

原文 ASP.NET MVC 5 学习教程:快速入门 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 通过控制器访问模型的数据 生成的代码详解 使用 SQL Server LocalDB Edit方法和Edit视图详解 添加查询 Entity Framework 数据迁移之添加字段 添加验证 Details 和 Delete 方法详解 本教程将介绍如何使用Visual Studio Express 201

ASP.NET MVC 5 学习教程:添加视图

原文 ASP.NET MVC 5 学习教程:添加视图 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 通过控制器访问模型的数据 生成的代码详解 使用 SQL Server LocalDB Edit方法和Edit视图详解 添加查询 Entity Framework 数据迁移之添加字段 添加验证 Details 和 Delete 方法详解 在本节内容中,我们将修改HelloWorldController类,使

ASP.NET MVC 5 学习教程:通过控制器访问模型的数据

原文 ASP.NET MVC 5 学习教程:通过控制器访问模型的数据 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 通过控制器访问模型的数据 生成的代码详解 使用 SQL Server LocalDB Edit方法和Edit视图详解 添加查询 Entity Framework 数据迁移之添加字段 添加验证 Details 和 Delete 方法详解 在本节中,你将新建一个MoviesController