Android - 单击列表视图中的任何声音时显示进度条

时间:2018-02-13 05:07:54

标签: android listview android-progressbar

this is what i want but dont know how to do it. 以下是我的主要活动的代码。我有一个声音剪辑的列表视图,我想在我们点击任何声音时显示进度条和按钮,进度条应指示相应声音剪辑的进度和按钮应该更改为停止图标。我正在使用带有arraylist.i的adapterview附加了该图像

  public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,NavigationView.OnNavigationItemSelectedListener {
private ArrayList<String> songList;
private ListView songView;
SearchView searchView;
private MediaPlayer mp;
ProgressBar progressBar;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        songList = new ArrayList<>();
        mp = new MediaPlayer();
        songView = findViewById(R.id.song_list);
        progressBar=findViewById(R.id.progressBar);
        try {
            getSongList();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, songList);
        songView.setAdapter(listAdapter);
        songView.setOnItemClickListener(this);
        registerForContextMenu(songView);
        Intent serviceIntent =
                new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");
        bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);


        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        // code for scroll to top button
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                songView.setSelection(0);
            }
        });

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    IInAppBillingService mService;

    ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name,
                                       IBinder service) {
            mService = IInAppBillingService.Stub.asInterface(service);
        }
    };

    public void getSongList() throws IllegalAccessException {


        Field[] fields = R.raw.class.getFields();
        // loop for every file in raw folder
        for (Field field : fields) {

            int rid = field.getInt(field);

            // Use that if you just need the file name
            String filename = field.getName();
            songList.add(filename);
            songList
        }
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    public void onDestroy() {

        super.onDestroy();
        if (mService != null) {
            unbindService(mServiceConn);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                return true;
            }
        });
        return super.onCreateOptionsMenu(menu);

    }

    @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();


        //noinspection SimplifiableIfStatement
        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_remove_ads) {


        } else if (id == R.id.nav_share) {
            try {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("text/plain");
                i.putExtra(Intent.EXTRA_SUBJECT, "SoundBoard");
                String sAux = "\nGet this app on Google Play\n\n";
                sAux = sAux + "https://play.google.com/store/apps/details?id=com.facebook.orca \n\n";
                i.putExtra(Intent.EXTRA_TEXT, sAux);
                startActivity(Intent.createChooser(i, "choose one"));
            } catch (Exception e) {
                //e.toString();
            }
        } else if (id == R.id.nav_rate) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.facebook.orca"));
            startActivity(intent);
        } else if (id == R.id.about_us) {

        }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        // selected item
        TextView txtView = (TextView) view;
        String fname = txtView.getText().toString().toLowerCase();

        int resID = getResources().getIdentifier(fname, "raw", getPackageName());
        Toast.makeText(getApplicationContext(), "Hello Javatpoint"+getResources().getResourceName(resID), Toast.LENGTH_SHORT).show();

        MediaPlayer mediaPlayer = MediaPlayer.create(this, resID);
        mediaPlayer.start();
        if (mp.isPlaying()) {
            mp.reset();

        }


    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        if (v.getId() == R.id.song_list) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu_list, menu);
        }
    }


    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        View view = info.targetView;
        TextView txtView = (TextView) view;
        String fname = txtView.getText().toString().toLowerCase();
        int resID = getResources().getIdentifier(fname, "raw", getPackageName());
        switch (item.getItemId()) {
            case R.id.share:
                // add stuff here
                //String sharepath=
                //Uri uri = Uri.parse(view.getResources().getResourceName(resID));
                //String sharePath = Environment.getExternalStorageDirectory().getPath()
                 //       + "/Soundboard/Ringtones/custom_ringtone.ogg";
                //Uri uri = Uri.parse(sharePath);
                // Use this to load the file
                Uri uri=Uri.parse("file://" +getResources().getResourceEntryName(resID)+".mp3");
                String str=("content://"+uri);
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("audio/mp3");
                share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                share.putExtra(Intent.EXTRA_STREAM,uri);
                startActivity(Intent.createChooser(share, "Share Sound File"));
                return true;
            case R.id.notification:
                // edit stuff here
                uri=Uri.parse("file://" +getResources().getResourceEntryName(resID)+".mp3");
                str=("content://"+uri);
                Toast.makeText(getApplicationContext(), ""+uri, Toast.LENGTH_SHORT).show();
                return true;
            case R.id.ringtone:
                // remove stuff here

                return true;
            default:
                return super.onContextItemSelected(item);
        }

    }
  }

0 个答案:

没有答案