0
我正在開發具有兩個選項卡的應用程序。標籤1中的片段記錄音頻,標籤2中的片段顯示文件列表。現在我想每次單擊停止按鈕時都會刷新SwipeRefreshLayout
中的列表。我做了一個接口,以便片段1調用活動,現在活動必須調用片段2中的onRefresh()
或調用onRefresh()
的片段2中的任何其他方法。我沒有那個工作。我不能使用調用選項卡片段來自活動的方法
Fragment fragment=getSupportFragmentManager.findFragmentbyId()
因爲我沒有在XML中的片段,這意味着沒有ID。
任何幫助?
這裏的代碼:
活動:
public class RecordingActivity extends AppCompatActivity implements Item_recorded{
private Toolbar toolbar;
TabLayout tabLayout;
PopupWindow mPopupWindow;
ViewPager pager;
View pview;
static Boolean Saved;
@Override
protected void onCreate(Bundle savedInstanceState) {
Saved= false;
ShowList.mp = new MediaPlayer();
super.onCreate(savedInstanceState);
if (getSharedPreferences("jdev.voicerecorder", Context.MODE_PRIVATE).getBoolean("navBar", false)==true){
getWindow().setNavigationBarColor(getResources().getColor(R.color.primary));
}
setContentView(R.layout.activity_recording);
toolbar = (Toolbar) findViewById(R.id.toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabHost);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_recording,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getItemId()==R.id.information){
DialogFragment newFragment = new madeby();
newFragment.show(getSupportFragmentManager(), "missiles");
}
if (item.getItemId()==R.id.settings){
Intent intent=new Intent(this, Settings.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
if (getSharedPreferences("jdev.voicerecorder", Context.MODE_PRIVATE).getBoolean("navBar", false)==true){
getWindow().setNavigationBarColor(getResources().getColor(R.color.primary));
}
}
@Override
public void recorded() {
FragmentManager fragmentManager=getSupportFragmentManager();
}
public class SectionPagerAdapter extends FragmentStatePagerAdapter {
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
default:
return new Record();
case 1:
return new ShowList();
}
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
String settitle1 = getResources().getString(R.string.recordTab);
String settitle2 = getResources().getString(R.string.recordsTab);
String tabtitles[] = new String[] { settitle1, settitle2 };
return tabtitles[position];
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".RecordingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabHost" />
片段2:
public class ShowList extends ListFragment implements SwipeRefreshLayout.OnRefreshListener, MediaPlayer.OnPreparedListener{
private String path;
static SwipeRefreshLayout mSwipeLayout;
private ArrayAdapter<String> adapter;
private ArrayList<String> values;
private String[] list;
private TextView durationT;
private String path2;
private String path3;
private File dir3;
private File dir2;
private File dir;
private View p;
private int length;
static MediaPlayer mp;
private long currentNow;
static boolean isPlaying;
SeekBar mSeekBar;
int duration;
static TextView mTime;
ArrayList<String> mdurationlist;
MediaPlayer mp1;
String durations;
static CardView cw;
int cx;
int cy;
int initialRadius;
Animator anim;
boolean cardOutforplay;
boolean forRRunnable;
public ShowList() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
p= inflater.inflate(R.layout.fragment_show_list, container, false);
mTime=(TextView)p.findViewById(R.id.playerTime);
mTime.setText("00:00/00:00");
mSeekBar=(SeekBar)p.findViewById(R.id.progressbar);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser){
mp.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mSwipeLayout=(SwipeRefreshLayout)p.findViewById(R.id.swipelayout);
mSwipeLayout.setOnRefreshListener(this);
mSwipeLayout.setColorSchemeResources(R.color.accent);
cw=(CardView)p.findViewById(R.id.progresscard);
cw.setVisibility(View.INVISIBLE);
// Use the current directory as title
path = Environment.getExternalStorageDirectory()
+File.separator
+"Recorded"
+File.separator;
if (getActivity().getIntent().hasExtra("path")) {
path = getActivity().getIntent().getStringExtra("path");
}
// Read all files sorted into the values-array
values = new ArrayList<String>();
dir = new File(path);
list = dir.list();
if (list != null) {
for (String file : list) {
if (file.endsWith(".wav")){
values.add(file);
}
else if (file.endsWith(".3gp")){
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);
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (RecordingActivity.Saved==true){
onRefresh();
interrupt();
RecordingActivity.Saved=false;
}
if (!RecordingActivity.Saved){
CountDownTimer countDownTimer=new CountDownTimer(300,1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
interrupt();
}
}.start();
}
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
return p;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
View myView = p.findViewById(R.id.progresscard);
if (myView.getVisibility()==View.VISIBLE){
myView.setVisibility(View.INVISIBLE);
}
path3 = Environment.getExternalStorageDirectory()
+ File.separator
+ "Recorded"
+ File.separator;
dir3 = new File(path3);
list = dir3.list();
File selected = new File(path + values.get(position));
selected.delete();
adapter.notifyDataSetChanged();
onRefresh();
Toast.makeText(getActivity().getApplicationContext(), R.string.deleted, Toast.LENGTH_SHORT).show();
if (myView.getVisibility()==View.VISIBLE){
myView.setVisibility(View.INVISIBLE);
}
return false;
}
});
}
@Override
public void onListItemClick(ListView l, View v, final int position, long id) {
forRRunnable= false;
View myView= p.findViewById(R.id.progresscard);
mp=new MediaPlayer();
Log.v("mp.isPlaying()", "mp.isPlaying==" +mp.isPlaying());
mSeekBar.setProgress(0);
cardOutforplay= false;
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP) {
if (!isPlaying){
// previously invisible view
int cx = myView.getMeasuredWidth()/2;
int cy = myView.getMeasuredHeight()/2;
// get the final radius for the clipping circle
int finalRadius = Math.max(myView.getWidth(), myView.getHeight())/2;
Animator anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
myView.setVisibility(View.VISIBLE);
anim.start();
}
}
if (Build.VERSION.SDK_INT< Build.VERSION_CODES.LOLLIPOP){
if (!isPlaying){
myView.setVisibility(View.VISIBLE);
}
}
if (isPlaying){
cardOutforplay = true;
mp.stop();
mp.release();
}
String musicURI = path
+ values.get(position);
try {
mp.setDataSource(musicURI);
} catch (IOException | IllegalStateException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IOException | IllegalStateException e) {
e.printStackTrace();
}
mp.setOnPreparedListener(this);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
final View myView = p.findViewById(R.id.progresscard);
isPlaying=false;
mTime.setText("00:00/00:00");
if (!cardOutforplay) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// get the center for the clipping circle
cx = myView.getMeasuredWidth()/2;
cy = myView.getMeasuredHeight()/2;
initialRadius = myView.getWidth();
anim =
ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, 0);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
myView.setVisibility(View.INVISIBLE);
mSeekBar.setProgress(0);
}
});
anim.start();
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Toast.makeText(getActivity(), "Visibility is: " + myView.getVisibility(), Toast.LENGTH_SHORT).show();
myView.setVisibility(View.INVISIBLE);
mSeekBar.setProgress(0);
}
}
}
});
}
private Runnable onEverySecond=new Runnable() {
@Override
public void run() {
if (forRRunnable){
if (mSeekBar!=null){
if (mp.isPlaying()){
mSeekBar.setProgress(mp.getCurrentPosition());
}
}
if (mp.isPlaying()){
mSeekBar.postDelayed(onEverySecond,1000);
updateTime();
}
}
}
};
@Override
public void onRefresh() {
path2 = Environment.getExternalStorageDirectory()
+ File.separator
+ "Recorded"
+ File.separator;
dir2=new File(path2);
values.clear();
adapter.notifyDataSetChanged();
list=dir2.list();
for (String file:list){
if (file.endsWith(".wav")){
values.add(file);
adapter.notifyDataSetChanged();
}
else if (file.endsWith(".3gp")){
values.add(file);
adapter.notifyDataSetChanged();
}
}
Collections.sort(values);
mSwipeLayout.setRefreshing(false);
}
@Override
public void onPrepared(MediaPlayer mp) {
duration = mp.getDuration();
mSeekBar.setMax(duration);
mp.setVolume(1.0f, 1.0f);
mp.start();
if (mp.isPlaying()){
forRRunnable=true;
mSeekBar.setProgress(0);
isPlaying = true;
mSeekBar.postDelayed(onEverySecond, 1000);
}
}
private void updateTime(){
do {
int current = mp.getCurrentPosition();
int dSeconds = (int) (duration/1000) % 60 ;
int dMinutes = (int) ((duration/(1000*60)) % 60);
int dHours = (int) ((duration/(1000*60*60)) % 24);
int cSeconds = (int) (current/1000) % 60 ;
int cMinutes = (int) ((current/(1000*60)) % 60);
int cHours = (int) ((current/(1000*60*60)) % 24);
if(dHours == 0){
mTime.setText(String.format("%02d:%02d/%02d:%02d", cMinutes, cSeconds, dMinutes, dSeconds));
}else{
mTime.setText(String.format("%02d:%02d:%02d/%02d:%02d:%02d", cHours, cMinutes, cSeconds, dHours, dMinutes, dSeconds));
}
try{
Log.d("Value: ", String.valueOf((int) (current * 100/duration)));
if(mSeekBar.getProgress() >= 100){
break;
}
}catch (Exception e) {}
}while (mSeekBar.getProgress() <= 100);
}
public void answer(String answer){
getActivity().getWindow().setNavigationBarColor(R.color.primary);
onRefresh();
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="jdev.voicerecorder.ShowList"
android:id="@+id/fragmentshowlistlayout">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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:id="@+id/progresscard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:visibility="invisible"
android:layout_alignParentBottom="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="64dp">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:id="@+id/progressbar"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:paddingTop="4dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textSize="16dp"
android:id="@+id/playerTime"
android:fontFamily="sans-serif-light"
android:layout_gravity="right"
android:layout_below="@+id/progressbar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="right"
android:text="00:00/00:00"
android:paddingRight="16dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
非靜態方法「onRefresh()」不能從靜態上下文 –
引用聲明瞭這樣一個片段? ShowListFragment mShowListFragment = new ShowListFragment()? –