gpt4 book ai didi

java - 使用 SwipeRefreshLayout 刷新 ListView

转载 作者:行者123 更新时间:2023-11-30 02:15:32 34 4
gpt4 key购买 nike

嘿,我正在开发一个 AudioRecorder 应用程序(我是 Android 开发的新手)。一个“Tab”用于录制文件,另一个用于播放文件。问题是列表的适配器在 Activity 开始时捕获数据,而不是在记录之后。所以我在onRefresh()方法中尝试让Swiperefreshlayout刷新数据。但我不让它工作!我觉得问题出在这里

for (String file:list){
if (file.endsWith(".3pg")){
values.add(file);
adapter.notifyDataSetChanged();
}
}

在 onRefresh() 中。

FragmentAllRecords.java:

package com.julian.recorder;


import android.app.LauncherActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.zip.Inflater;


/**
* A simple {@link Fragment} subclass.
*/
public class FragmentAllRecords extends ListFragment implements SwipeRefreshLayout.OnRefreshListener {
private String path;
private SwipeRefreshLayout mSwipeLayout;
private String musicURI;
private MediaPlayer mp;
private CardView mMediaCard;
private ProgressBar mMediaProgress;
private ArrayAdapter<String> adapter;
private ArrayList<String> values;
private String[] list;




public FragmentAllRecords() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View p= inflater.inflate(R.layout.fragment_fragment_all_records, container, false);
mSwipeLayout=(SwipeRefreshLayout)p.findViewById(R.id.swipelayout);
mSwipeLayout.setOnRefreshListener(this);



mMediaCard=(CardView)p.findViewById(R.id.mediaCard);
mMediaCard.setVisibility(View.GONE);

mp=new MediaPlayer();



// Use the current directory as title
path = Environment.getExternalStorageDirectory()
+File.separator
+Environment.DIRECTORY_MUSIC
+File.separator;
if (getActivity().getIntent().hasExtra("path")) {
path = getActivity().getIntent().getStringExtra("path");
}

// Read all files sorted into the values-array
values = new ArrayList<String>();
File dir = new File(path);
list = dir.list();
if (list != null) {
for (String file : list) {
if (!file.endsWith(".3pg")) {
values.add(file);


}
}
}
Collections.sort(values);

// Put the data into the list
adapter = new ArrayAdapter<String>(getActivity(),
R.layout.recording_list_item, R.id.listText, values);
setListAdapter(adapter);





return p;
}



@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mMediaCard.setVisibility(View.VISIBLE);
int duration=mp.getDuration();
int currposition=mp.getCurrentPosition();





musicURI=path
+values.get(position);

mp.getDuration();
try {
mp.setDataSource(musicURI);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();


}


@Override
public void onRefresh() {
values.clear();
adapter.notifyDataSetChanged();

for (String file:list){
if (file.endsWith(".3pg")){
values.add(file);
adapter.notifyDataSetChanged();
}
}

mSwipeLayout.setRefreshing(false);
}


}

fragment_fragment_all_records.xml:

<FrameLayout 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" tools:context="com.julian.recorder.FragmentAllRecords">

<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipelayout">

<ListView android:id="@android:id/list" android:layout_width="match_parent"
android:layout_height="wrap_content" />

</android.support.v4.widget.SwipeRefreshLayout>

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="bottom"
android:visibility="visible"
android:elevation="20dp"
android:id="@+id/mediaCard">



<Button
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="center"
android:id="@+id/mStop"
android:src="@drawable/pause_circle"
android:background="@drawable/pause_circle"

android:visibility="visible"/>

<Button
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="center"
android:id="@+id/mResume"
android:visibility="gone"
android:background="@drawable/play_circle"/>

<Button
android:layout_width="36dp"
android:background="@drawable/skip_next"
android:layout_height="36dp"
android:layout_gravity="center"
android:layout_marginLeft="64dp"
android:id="@+id/mNext" />

<Button
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:id="@+id/mPrevious"
android:layout_marginRight="64dp"
android:background="@drawable/skip_previous"/>


</android.support.v7.widget.CardView>

(也许这也是需要的。不知道...)Mainactivity.java:(我称之为RecordingActivity)

    package com.julian.recorder;

import android.app.Activity;
import android.content.Context;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.view.View;
import android.widget.Button;
import android.support.v4.view.PagerAdapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.media.AudioRecord;
import android.widget.Toast;
import android.widget.ArrayAdapter;
import android.app.ListActivity;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.getbase.floatingactionbutton.FloatingActionButton;
import com.getbase.floatingactionbutton.FloatingActionsMenu;
import com.getbase.floatingactionbutton.AddFloatingActionButton;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;


public class RecordingActivity extends ActionBarActivity implements MaterialTabListener{

private Toolbar toolbar;

MaterialTabHost tabHost;
ViewPager pager;
ViewPagerAdapter pagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recording);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


tabHost = (MaterialTabHost) this.findViewById(R.id.tabHost);
pager = (ViewPager) this.findViewById(R.id.pager);
pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
pager.setAdapter(pagerAdapter);



pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
tabHost.setSelectedNavigationItem(position);
}
});

for (int i = 0; i < pagerAdapter.getCount(); i++) {
tabHost.addTab(
tabHost.newTab()
.setText(pagerAdapter.getPageTitle(i))
.setTabListener(this)
);
}






}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recording, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();



return super.onOptionsItemSelected(item);
}


@Override
public void onTabSelected(MaterialTab materialTab) {
pager.setCurrentItem(materialTab.getPosition());
}

@Override
public void onTabReselected(MaterialTab materialTab) {

}

@Override
public void onTabUnselected(MaterialTab materialTab) {

}





}

最佳答案

我认为您错误地在 file.endsWith(".3pg") 处提供了文件扩展名,它应该是“.3gp”,这是您生成的音频文件的正确扩展名。

关于java - 使用 SwipeRefreshLayout 刷新 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29431143/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com