android下拉刷新带自定义声音

来源:互联网 发布:js 银行卡格式 编辑:程序博客网 时间:2024/05/14 18:06
写这篇文章时,我android开发已经半年多了,今天刚做完下拉刷新带声音,比如你R.raw导入的声音是某人唱的也可以哦,但是下拉刷新下声音不会太长就1.2秒的事呵呵    。废话不说直接看代码!

public class LauncherActivity extends ListActivity {//主页面activity点击第一个listview进入下拉刷新滑动带声音的activity

 public static final String[] options = { "下拉刷新滑动带声音", "ExpandableListView(QQ分组)", "GridView(下拉刷新出现格子数据)", "WebView", "ScrollView",
   "Horizontal ScrollView(左右侧滑)", "ViewPager", "ListView Fragment", "WebView Advanced", "ListView in ViewPager","学习","很重要" };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options));
 }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  Intent intent;

  switch (position) {
   default:
   case 0:
    intent = new Intent(this, PullToRefreshListActivity.class);
    break;
   case 1:
    intent = new Intent(this, PullToRefreshExpandableListActivity.class);
    break;
   case 2:
    intent = new Intent(this, PullToRefreshGridActivity.class);
    break;
   case 3:
    intent = new Intent(this, PullToRefreshWebViewActivity.class);
    break;
   case 4:
    intent = new Intent(this, PullToRefreshScrollViewActivity.class);
    break;
   case 5:
    intent = new Intent(this, PullToRefreshHorizontalScrollViewActivity.class);
    break;
   case 6:
    intent = new Intent(this, PullToRefreshViewPagerActivity.class);
    break;
   case 7:
    intent = new Intent(this, PullToRefreshListFragmentActivity.class);
    break;
   case 8:
    intent = new Intent(this, PullToRefreshWebView2Activity.class);
    break;
   case 9:
    intent = new Intent(this, PullToRefreshListInViewPagerActivity.class);
    break;
  }

  startActivity(intent);
 }

}

//看配置文件  simple_list_item_1.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>

核心代码

public final class PullToRefreshListActivity extends ListActivity {

 static final int MENU_MANUAL_REFRESH = 0;
 static final int MENU_DISABLE_SCROLL = 1;
 static final int MENU_SET_MODE = 2;
 static final int MENU_DEMO = 3;

 private LinkedList<String> mListItems;
 private PullToRefreshListView mPullRefreshListView;
 private ArrayAdapter<String> mAdapter;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_ptr_list);

  mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);

  // Set a listener to be invoked when the list should be refreshed.
  mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
   @Override
   public void onRefresh(PullToRefreshBase<ListView> refreshView) {
    String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
      DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

    // Update the LastUpdatedLabel
    refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

    // Do work to refresh the list here.
    new GetDataTask().execute();
   }
  });

  // Add an end-of-list listener
  mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

   @Override
   public void onLastItemVisible() {
    Toast.makeText(PullToRefreshListActivity.this, "End of List!", Toast.LENGTH_SHORT).show();
   }
  });

  ListView actualListView = mPullRefreshListView.getRefreshableView();

  // Need to use the Actual ListView when registering for Context Menu
  registerForContextMenu(actualListView);

  mListItems = new LinkedList<String>();
  mListItems.addAll(Arrays.asList(mStrings));

  mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);

  /**
   * Add Sound Event Listener
   */
  SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this);
  soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
  soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
  soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
  mPullRefreshListView.setOnPullEventListener(soundListener);

  // You can also just use setListAdapter(mAdapter) or
  // mPullRefreshListView.setAdapter(mAdapter)
  actualListView.setAdapter(mAdapter);
 }

 private class GetDataTask extends AsyncTask<Void, Void, String[]> {

  @Override
  protected String[] doInBackground(Void... params) {
   // Simulates a background job.
   try {
    Thread.sleep(4000);
   } catch (InterruptedException e) {
   }
   return mStrings;
  }

  @Override
  protected void onPostExecute(String[] result) {
   mListItems.addFirst("Added after refresh...");
   mAdapter.notifyDataSetChanged();

   // Call onRefreshComplete when the list has been refreshed.
   mPullRefreshListView.onRefreshComplete();

   super.onPostExecute(result);
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  menu.add(0, MENU_MANUAL_REFRESH, 0, "Manual Refresh");
  menu.add(0, MENU_DISABLE_SCROLL, 1,
    mPullRefreshListView.isScrollingWhileRefreshingEnabled() ? "Disable Scrolling while Refreshing"
      : "Enable Scrolling while Refreshing");
  menu.add(0, MENU_SET_MODE, 0, mPullRefreshListView.getMode() == Mode.BOTH ? "Change to MODE_PULL_DOWN"
    : "Change to MODE_PULL_BOTH");
  menu.add(0, MENU_DEMO, 0, "Demo");
  return super.onCreateOptionsMenu(menu);
 }

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;

  menu.setHeaderTitle("Item: " + getListView().getItemAtPosition(info.position));
  menu.add("Item 1");
  menu.add("Item 2");
  menu.add("Item 3");
  menu.add("Item 4");

  super.onCreateContextMenu(menu, v, menuInfo);
 }

 @Override
 public boolean onPrepareOptionsMenu(Menu menu) {
  MenuItem disableItem = menu.findItem(MENU_DISABLE_SCROLL);
  disableItem
    .setTitle(mPullRefreshListView.isScrollingWhileRefreshingEnabled() ? "Disable Scrolling while Refreshing"
      : "Enable Scrolling while Refreshing");

  MenuItem setModeItem = menu.findItem(MENU_SET_MODE);
  setModeItem.setTitle(mPullRefreshListView.getMode() == Mode.BOTH ? "Change to MODE_FROM_START"
    : "Change to MODE_PULL_BOTH");

  return super.onPrepareOptionsMenu(menu);
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

  switch (item.getItemId()) {
   case MENU_MANUAL_REFRESH:
    new GetDataTask().execute();
    mPullRefreshListView.setRefreshing(false);
    break;
   case MENU_DISABLE_SCROLL:
    mPullRefreshListView.setScrollingWhileRefreshingEnabled(!mPullRefreshListView
      .isScrollingWhileRefreshingEnabled());
    break;
   case MENU_SET_MODE:
    mPullRefreshListView.setMode(mPullRefreshListView.getMode() == Mode.BOTH ? Mode.PULL_FROM_START
      : Mode.BOTH);
    break;
   case MENU_DEMO:
    mPullRefreshListView.demo();
    break;
  }

  return super.onOptionsItemSelected(item);
 }

 private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
   "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
   "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
   "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
   "Allgauer Emmentaler" };
}

//配置文件名字:activity_ptr_list.xml

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

<!--     The PullToRefreshListView replaces a standard ListView widget. -->

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true" />

</LinearLayout>

//核心代码

/**
   * Add Sound Event Listener
   */
  SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this);
  soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
  soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
  soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
  mPullRefreshListView.setOnPullEventListener(soundListener);

//以后要在APP加一些声音这个可以直接复制粘贴了

 

 

 

 

 

 

 


 

1 0
原创粉丝点击