-1

我在单个活动 StackView 和 Gridview 以及 2 个相应的适配器中有两个视图。当我单击 StackView 项目时,它应该翻转 Gridview 中的网格项目,反之亦然?

4

2 回答 2

-1

// 活动类

  public class SampleActivity extends Activity implements    
  OnGridChangeListener {

  public void onCreate(bundle){
 // replace this with your adapter class. 
 Adapter adapter = new adapter(this); 
 }

 @override
 public void OnGridChange(){
   //here you go.
  // write code to do what you want.
}

    // interface to communicate with activity
  public interface OnGridChangeListener {
       public void OnGridChange()
   }


   // adaptor class
  public class Adaptor extends "you apapter class to extend"{

  OnGridChangeListener onGridChangeListener ;

  public Adapter(OnGridChangeListener  listener){
     onGridChangeListener =listener
   }

 public getView(){

   public void onclick(){
      onGridChangeListener.OnGridChange("pass you data");
}
}

 }
于 2017-04-09T05:23:57.507 回答
-2

根据您的问题,这就是我得到的-

你有一个 Activity 有两个独立的视图,它们有自己的适配器。因此,当其中一个适配器发生更改时,您希望将其反映到另一个适配器中。

您查询的简单解决方案是 -

  1. 当第一个适配器发生更改时,您会将更改反映到活动中。之后调用第二个适配器中的函数以反映您想要在第二个适配器中进行的更改。
  2. 为此,您必须在第一个适配器中定义一个接口并实现这是活动。
  3. 当第一个适配器更改调用接口方法时,这将反映在活动中。
  4. 然后在第二个适配器中调用方法来做你想要的改变。

代码示例 -

MainActivity.Java

public class MainActivity extends AppCompatActivity implements FirstAdapter.callBackMethods {

FirstAdapter firstAdapter;
SecondAdapter secondAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    firstAdapter = new FirstAdapter(MainAtivity.this.getApplicationContext());
    //do all the declaration and operation of first adapter. Pass context along with your required params.

    secondAdapter = new SecondAdapter();
    //do all the declaration and operation of second adapter.
  }

//callback method of first adapter
@override
public void callback(){
    //changes have been done in FirstAdapter and this methos is fired.
    //now do do the changes in SecondAdapter as per req.
    if(secondAdapter != null){
       secondAdapter.reflectChanges();
    }
  }
}

FirstAdapter.class

我以 recylerview 适配器为例。

public class FirstAdapter extends RecyclerView.Adapter<FirstAdapter.ViewHolder>{

public FirstAdapter(Context context){
  this.context=context;
  }

/*
  all the boilerplate codes and business logic
 */

//when you want to reflect the changes
callBackMethods callBackMethods = (callBackMethods) context;
callBackMethods.callback();
//this will fireup the implementation in the MainActivity.

public interface callBackMethods{
  public void callback();
  }
}

SecondAdapter.class

这是将反映更改的地方。

public class SecondAdapter extends RecyclerView.Adapter<SecondAdapter.ViewHolder>{

/*
  all the boilerplate codes and business logic
 */

public void reflectChanges(){
   /*
    This method will be called from the MainActivity. pass whatever params you want to pass from Activity and do the changes you want to do in the SecondAdapter.
    */
  }
}

我希望这能解决你的问题。快乐的编码...

于 2017-04-09T06:33:41.623 回答