我想更改android网格视图行项目的按钮,当我点击第一行时,我可以将按钮(播放)更改为(暂停),然后当我转到另一行时,我也可以更改播放暂停,但我需要自动更改前一行暂停,因为它现在仍在播放。
我已经尝试了下面的代码但没有效果。
主网格XML
<RelativeLayout 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=".MainActivity" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:numColumns="1"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:clipToPadding="false"/>
</RelativeLayout>
行XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageViewMain"
android:layout_width="120dp"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="@drawable/img4" />
<TextView
android:id="@+id/textViewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageViewMain"
android:layout_margin="5dp"
android:layout_toRightOf="@+id/imageViewMain"
android:text="PRİNCESS BİRTHDAY"
android:textColor="#000"
android:textSize="24dp" />
<TextView
android:id="@+id/textViewYazar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#412204"
android:textStyle="bold"
android:textSize="10sp"
android:text="Yazar : "
android:layout_below="@+id/textViewMain"
android:layout_alignLeft="@+id/textViewMain"
android:layout_alignStart="@+id/textViewMain" />
<TextView
android:id="@+id/textViewYazarName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yazar adı"
android:textColor="#412204"
android:textSize="10sp"
android:layout_below="@+id/textViewMain"
android:layout_toRightOf="@+id/textViewYazar"
android:layout_toEndOf="@+id/textViewYazar"
android:layout_marginLeft="26dp"/>
<TextView
android:id="@+id/textViewSeslendiren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#412204"
android:textStyle="bold"
android:textSize="10sp"
android:text="Seslendiren : "
android:layout_below="@+id/textViewYazar"
android:layout_alignLeft="@+id/textViewMain"
android:layout_alignStart="@+id/textViewMain" />
<TextView
android:id="@+id/textViewSeslendirenName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Seslendiren Adı"
android:textColor="#412204"
android:textSize="10sp"
android:layout_below="@+id/textViewYazarName"
android:layout_toRightOf="@+id/textViewYazar"
android:layout_alignStart="@+id/textViewYazarName"/>
<TextView
android:id="@+id/textViewSizeDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#412204"
android:textStyle="bold"
android:textSize="10sp"
android:text="120 MB / 130 Mins"
android:layout_below="@+id/textViewSeslendirenName"
android:layout_alignLeft="@+id/textViewMain"
android:layout_alignStart="@+id/textViewMain" />
<Button
android:id="@+id/buttonPlay"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_above="@+id/imageViewBottomLine"
android:layout_alignLeft="@+id/textViewMain"
android:background="@drawable/ic_action_play"/>
<ImageView
android:id="@+id/imageViewTL"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/tl_simgesi"
android:layout_alignTop="@+id/textViewPrice"
android:layout_toLeftOf="@+id/textViewPrice"/>
<TextView
android:id="@+id/textViewPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2.70"
android:textColor="#E85404"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginRight="50dp"
android:layout_marginBottom="10dp"
android:layout_above="@+id/imageViewBottomLine"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_action_basket_dark"
android:layout_above="@+id/imageViewBottomLine"
android:layout_alignParentRight="true"
android:onClick="onAddToCard"/>
<ImageView
android:id="@+id/imageViewBottomLine"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/imageViewMain"
android:scaleType="fitXY"
android:src="@drawable/top_line" />
</RelativeLayout>
查看适配器GetView代码
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
LayoutInflater inflator = activity.getLayoutInflater();
imageLoader = new ImageLoader(activity.getApplicationContext());
if (convertView == null) {
holder = new ViewHolder();
convertView = inflator.inflate(R.layout.gridrow2, null);
holder.imgViewMain = (ImageView) convertView.findViewById(R.id.imageViewMain);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textViewMain);
holder.txtViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.txtViewCast = (TextView) convertView.findViewById(R.id.textViewSeslendirenName);
holder.txtViewAuthor = (TextView) convertView.findViewById(R.id.textViewYazarName);
holder.txtViewSizeDuration = (TextView) convertView.findViewById(R.id.textViewSizeDuration);
holder.play = (Button) convertView.findViewById(R.id.buttonPlay);
holder.addcart = (Button) convertView.findViewById(R.id.buttonAdd);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
imageLoader.DisplayImage(urlList.get(position), holder.imgViewMain);
if (title.get(position).length() > 30) {
holder.txtViewTitle.setTextSize(20);
}
holder.txtViewTitle.setText(title.get(position));
holder.txtViewPrice.setText(price.get(position).replaceAll(" TL", ""));
holder.txtViewCast.setText(cast.get(position));
holder.txtViewAuthor.setText(author.get(position));
holder.txtViewSizeDuration.setText(size.get(position) + " / " + duration.get(position));
holder.imgViewMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(activity.getApplicationContext(), SubMenu.class);
in.putExtra("Title", title.get(position));
in.putExtra("Id", id.get(position));
in.putExtra("Author", author.get(position));
in.putExtra("Description", description.get(position));
in.putExtra("Img", urlList.get(position));
in.putExtra("Price", price.get(position));
in.putExtra("Size", size.get(position));
in.putExtra("Duration", duration.get(position));
in.putExtra("Publication", publication.get(position));
in.putExtra("Cast", cast.get(position));
activity.startActivity(in);
}
});
holder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.click == 0) {
stopPlayer(v.getContext(), position, previousPosition);
productId = id.get(position);
PlayExecute(v.getContext());
v.setBackground(v.getContext().getResources().getDrawable(R.drawable.ic_action_pause));
holder.click = 1;
previousPosition = position;
} else {
stopPlayer(v.getContext(), position, previousPosition);
v.setBackground(v.getContext().getResources().getDrawable(R.drawable.ic_action_play));
holder.click = 0;
}
}
});
holder.addcart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackground(v.getContext().getResources().getDrawable(R.drawable.ic_action_basket));
prfs = PreferenceManager.getDefaultSharedPreferences(activity);
productId = id.get(position);
AddExecute();
}
});
return convertView;
}
StopPlayer并更改上一个图标
public void stopPlayer(Context c, int current, int previous) {
System.out.println("current : " + current);
System.out.println("previous : " + previous);
if (mp != null && mp.isPlaying()) {
mp.stop();
}
if (current!=previous){
View v= this.getView(previousPosition, null, null);
Button b = (Button) v.findViewById(R.id.buttonPlay);
b.setBackground(c.getResources().getDrawable(R.drawable.ic_action_play));
this.notifyDataSetChanged();
mGridview.refreshDrawableState();
}
}
答案 0 :(得分:1)
而不是这样做
View v= this.getView(previousPosition, null, null);
更改您在项目点击方法上的可绘制
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Button b = (Button) view.findViewById(R.id.buttonPlay);
b.setBackground(c.getResources().getDrawable(R.drawable.ic_action_play));
your_adapter.notifyDataSetChanged();
}
});
我认为,这对你有用:)
答案 1 :(得分:0)
我认为你的问题是partial refresh
。在处理partial refresh problem
或ListView
时,我们都会遇到GridView
。至于你的情况,partial refresh
可能是这样的:
private void refreshPartially(int position){
int firstVisiblePosition = listview.getFirstVisiblePosition();
int lastVisiblePosition = listview.getLastVisiblePosition();
if(position>=firstVisiblePosition && position<=lastVisiblePosition){
View view = listview.getChildAt(position - firstVisiblePosition);
if(view.getTag() instanceof ViewHolder){
ViewHolder vh = (ViewHolder)view.getTag();
//holder.play.setBackgroundResource(resId);//Do something here.
...
}
}
}