Skip to content

Instantly share code, notes, and snippets.

@roideuniverse
Created September 13, 2015 00:54
Show Gist options
  • Save roideuniverse/3a0c824d1dc709332bea to your computer and use it in GitHub Desktop.
Save roideuniverse/3a0c824d1dc709332bea to your computer and use it in GitHub Desktop.
Seek Problem for someone
package roide.nanod.popularmovies;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
public class TrackPlayerAdapter extends RecyclerView.Adapter<TrackPlayerAdapter.MyHolder>
{
private final static String TAG = TrackPlayerAdapter.class.getSimpleName();
private Context context;
public TrackPlayerAdapter(Context context)
{
this.context = context;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
LinearLayout container = new LinearLayout(context);
container.setOrientation(LinearLayout.VERTICAL);
container.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
SeekBar seekbar = new SeekBar(context);
seekbar.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Button button = new Button(viewGroup.getContext());
button.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams
.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
button.setText("play");
seekbar.setTag("SEEKBAR");
button.setTag("BUTTON");
container.addView(seekbar);
container.addView(button);
return new MyHolder(container);
}
@Override
public void onBindViewHolder(MyHolder myHolder, int i)
{
myHolder.bind();
}
/**
* Get the row id associated with the specified position in the list.
*
* @param position The position of the item within the adapter's data set whose row id we want.
* @return The id of the item at the specified position.
*/
@Override
public long getItemId(int position)
{
return position;
}
@Override
public int getItemCount()
{
return 3;
}
public static class MyHolder extends RecyclerView.ViewHolder
{
private Handler mDurationHandler = new Handler();
private SeekBar mSpotifyMusicSeekBar;
private Button mToggleButton;
int totalProgress = 0;
public void updateSeekbarProgress()
{
mDurationHandler.postDelayed(mUpdateSeekbarTime, 100);
}
private Runnable mUpdateSeekbarTime = new Runnable()
{
public void run()
{
if(totalProgress <= 10000)
{
totalProgress += 100;
int progress = (int) ((float) totalProgress / 100);
mSpotifyMusicSeekBar.setProgress(progress);
mDurationHandler.postDelayed(this, 1000);
}
}
};
public MyHolder(View itemView)
{
super(itemView);
mSpotifyMusicSeekBar = (SeekBar)itemView.findViewWithTag("SEEKBAR");
mToggleButton = (Button) itemView.findViewWithTag("BUTTON");
}
public void bind()
{
mSpotifyMusicSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
Log.i(TAG, "onProgressChanged::" + progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
Log.i(TAG, "onStartTrackingTouch::" + seekBar.getProgress());
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
Log.i(TAG, "onStopTrackingTouch" + seekBar.getProgress());
}
});
mToggleButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
mSpotifyMusicSeekBar.setProgress(0);
mSpotifyMusicSeekBar.setMax(100);
updateSeekbarProgress();
}
});
}
}
}
/* Activity */
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
LinearLayoutManager manager = new LinearLayoutManager(layout.getContext());
manager.setOrientation(LinearLayoutManager.VERTICAL);
RecyclerView rv = new RecyclerView(layout.getContext());
rv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
rv.setLayoutManager(manager);
layout.addView(rv);
String[] colNames = {"_id", "music"};
MatrixCursor cursor = new MatrixCursor(colNames);
for(int i=0; i< 50; i++)
{
String[] vals = { String.valueOf(i), String.valueOf(i)};
cursor.addRow(vals);
}
TrackPlayerAdapter adapter = new TrackPlayerAdapter(this);
rv.setAdapter(adapter);
setContentView(layout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment